Home Backend Development PHP7 Summary of things you need to pay attention to after upgrading PHP to 7.2

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

Mar 21, 2021 pm 02:44 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)