php7.2.2的函数
php7.2.2相对于低版本的做了哪些变化?除了mysql_connect改成了mysqli_connect,是只改了这一个函数吗?还有别的吗?
<?php $b = array(); each($b); // Deprecated: The each() function is deprecated. This message will be suppressed on further calls<?php count(''); // Warning: count(): Parameter must be an array or an object that implements Countable
非常简单的两个原生态函数, 可能在5.2, 5.6, 7.0时使用都不会有问题, 但php7.2是会严格报异常的.
解决这两个函数, 需要重新定义函数. 如下.
<?php echo fun_adm_count(''); function fun_adm_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; } }
本函数出自qadmin, 目的是参数进入时, 先判断是数组或者对象再使用count, 其它的全部返回0.
<?php echo '<pre>'; $fruit = array(1,'a' => array('bbb'=>'ccc'), 'b' => 'banana', 'c' => 'cranberry'); while(list($key, $val) = fun_adm_each($fruit)){ echo "$key => $val "; } function fun_adm_each(&$array){ $result = array(); $key = key($array); if(!is_null($key)){ $val = $array[$key]; $result[1] = $val; $result['value'] = $val; $result[0] = $key; $result['key'] = $key; next($array); } return $result; }
此函数用人工方式操作了数据, 先取出key值, 再读取val值, 再next将指针前移一步. 返回数组格式.
-------------------------
<?php $newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);'); echo "New anonymous function: $newfunc "; echo $newfunc(2, M_E) . " "; // 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.
create_function函数也过时了.
解决方法是用闭包. 如:
<?php $fun = function ($str ){echo $str} $fun('Yuan');