Home >Backend Development >PHP Tutorial >What you need to know after upgrading PHP to 7.2

What you need to know after upgrading PHP to 7.2

小云云
小云云Original
2018-03-01 13:32:422679browse

The PHP version was recently upgraded from 7.1 to 7.2. There are some things we need to pay attention to after the upgrade. I hope this article can help everyone.

Pre-upgrade version:

PHP 7.1.14 (cli) (built: Feb  2 2018 08:42:59) ( NTS )Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.1.14, Copyright (c) 1999-2018, by Zend Technologies
    with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans

After-upgrade version:

PHP 7.2.2 (cli) (built: Feb 24 2018 17:51:12) ( ZTS DEBUG )Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.2, Copyright (c) 1999-2018, by Zend Technologies

After the upgrade, it was found that several frameworks had problems when using them. The main reasons were concentrated Some functions have been abandoned after 7.2. Here are some common problems:

1. Each function has been abandoned:

Previous version writing:

<?php
    $array = array();
    each($array);

    // Deprecated:  The each() function is deprecated. This message will be suppressed on further calls

In 7.2 The version will prompt that it is outdated. You can use foreach instead of the each method, or you can modify the each method yourself:

<?php
    function func_new_each(&$array){
       $res = array();       $key = key($array);       if($key !== null){
           next($array); 
           $res[1] = $res['value'] = $array[$key];           $res[0] = $res['key'] = $key;
       }else{           $res = false;
       }       return $res;
    }

2. When an invalid parameter is passed, the count() function will throw a warning warning:

Previous version writing

<?php
    count('');    // Warning:  count(): Parameter must be an array or an object that implements Countable

In version 7.2, type distinction will be strictly enforced. If the parameter type is incorrect, a warning will appear, so you need to pay attention to the value of the parameter when using the count method, but you can also pass it yourself Modify the method to replace (not recommended):

<?php
    function func_new_count($array_or_countable,$mode = COUNT_NORMAL){
        if(is_array($array_or_countable) || is_object($array_or_countable)){            return count($array_or_countable, $mode);
        }else{            return 0;
        }
    }

3. create_function is abandoned and can be replaced by an anonymous function:

Previous version writing:

<?php
    $newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');    echo "New anonymous function: $newfunc\n";    echo $newfunc(2, M_E) . "\n";    // outputs
    // New anonymous function: lambda_1
    // ln(2) + ln(2.718281828459) = 1.6931471805599

    // Warning This function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged.

In version 7.2 There will be a warning prompt, which can be modified to an anonymous function instead:

<?php
    $newfunc = function ($a,$b){
        return "ln($a) + ln($b) = " . log($a * $b);
    };    echo $newfunc(2, M_E) . "\n";

The above are a few temporary problems encountered after the upgrade. For other related modifications, please see the translation and arrangement made by the Lianjia product technical team: PHP7. 2 version guide.

Related recommendations:

php7 installation command record under Linux linux view php version linux php upgrade linux php mssq

The above is the detailed content of What you need to know after upgrading PHP to 7.2. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn