Home > Backend Development > PHP7 > body text

Summary of things you need to pay attention to after upgrading PHP to 7.2

coldplay.xixi
Release: 2023-02-17 21:06:01
forward
2092 people have browsed it

Summary of things you need to pay attention to after upgrading PHP to 7.2

The PHP version was recently upgraded from 7.1 to 7.2. Version before upgrade:

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
Copy after login

Version after upgrade:

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
Copy after login

Recommended (Free): PHP7

After the upgrade, I found that several frameworks had problems when using them. The main reason was that some functions were abandoned after 7.2, which are listed below Several 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
Copy after login

will prompt that it is outdated in version 7.2 , you can use foreach instead of each method, or you can modify each method yourself instead:

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

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

Previous version writing

<?php
    count(&#39;&#39;);    // Warning:  count(): Parameter must be an array or an object that implements Countable
Copy after login

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 Modify your own method to replace it (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;
        }
    }
Copy after login

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

Previous version writing:

<?php
    $newfunc = create_function(&#39;$a,$b&#39;, &#39;return "ln($a) + ln($b) = " . log($a * $b);&#39;);    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.
Copy after login

There will be a warning prompt in version 7.2, 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";
Copy after login

The above are a few problems temporarily encountered after the upgrade. For other related modifications, please see the Lianjia product technical team for details. Translation and organization: PHP7.2 version guide

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

Related labels:
source:PHP升级7.2之后需要注意的事情_Mider'S Blog-CSDN博客
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template