Home > Article > Backend Development > 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
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
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
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['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 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; } }
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.
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";
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!