从php5.4到php7.2,php7之后的变化尤其明显,而且越来越适合开发者的开发习惯,然而,今天,当服务器升级到7.2,突然发现到处报错,一一排查,可爱的count不想和大家玩了,记录一下7.2的变化,方便以后查询
1、最喜欢的count被修改,当传递一个无效参数时,count()函数将抛出warning警告:
之前版本写法
<?php
count('');
// Warning: count(): Parameter must be an array or an object that implements Countable
2、each函数已被废弃:
之前版本写法:
<?php
$array = array();
each($array);
// Deprecated: The each() function is deprecated. This message will be suppressed on further calls
在7.2版本中会提示过时,可以使用foreach替代each方法,也可以自己修改each方法替代:
<?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;
}
3、create_function被废弃,可以用匿名函数来代替:
之前的版本
<?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.
在7.2版本中会有警告提示,可修改为匿名函数来替代:
<?php
$newfunc = function ($a,$b){
return "ln($a) + ln($b) = " . log($a * $b);
};
echo $newfunc(2, M_E) . "\n";
以上就是升级之后暂时遇到的几个问题,其它相关修改可详看链家产品技术团队做的翻译及整理:PHP7.2 版本指南