從php5.4到php7.2欺嗤,php7之后的變化尤其明顯,而且越來越適合開發(fā)者的開發(fā)習(xí)慣卫枝,然而煎饼,今天,當(dāng)服務(wù)器升級到7.2校赤,突然發(fā)現(xiàn)到處報(bào)錯(cuò)吆玖,一一排查,可愛的count不想和大家玩了马篮,記錄一下7.2的變化沾乘,方便以后查詢
1、最喜歡的count被修改浑测,當(dāng)傳遞一個(gè)無效參數(shù)時(shí)翅阵,count()函數(shù)將拋出warning警告:
之前版本寫法
<?php
count('');
// Warning: count(): Parameter must be an array or an object that implements Countable
2、each函數(shù)已被廢棄:
之前版本寫法:
<?php
$array = array();
each($array);
// Deprecated: The each() function is deprecated. This message will be suppressed on further calls
在7.2版本中會提示過時(shí),可以使用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被廢棄,可以用匿名函數(shù)來代替:
之前的版本
<?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版本中會有警告提示槐雾,可修改為匿名函數(shù)來替代:
<?php
$newfunc = function ($a,$b){
return "ln($a) + ln($b) = " . log($a * $b);
};
echo $newfunc(2, M_E) . "\n";
以上就是升級之后暫時(shí)遇到的幾個(gè)問題,其它相關(guān)修改可詳看鏈家產(chǎn)品技術(shù)團(tuán)隊(duì)做的翻譯及整理:PHP7.2 版本指南