/**
function A(int $a, int &$b, int $c = 3) {echo $a."<br/>";echo $b."<br/>";$b += 1;echo $c."<br/>";return $a+$b;}$a = 1;$b = 1;echo A($a,$b)."<br/>";echo $b."<br/>";
//输出://1 输出$a的值是1//1 输出$b的值是1//3 输出$c的默认值是3//3 求A+B两数之和,$b被额外+1,返回值为3//2 由于$b 是传值引用,在add函数内被+1,显示2;
$result=function(int $a,int $b){return $a+$b;};echo $result(1,3)."<br/>";
//输出://4
$total=function(int $d)use($a,$b):string{return $d+$a+$b;};echo $total(3);
//输出://6 $a,$b为全局变量,进过A函数运算,为1和2
*函数作为参数传入进另一个函数中使用;PHP中有许多 “需求参数为函数” 的函数,像array_map,usort,call_user_func_array之类,他们执行传入的函数,然后直接将结果返回主函数
//求0-100之内的偶数data = range(0, 100);$arr = array_map(function(int $item){if($item % 2 === 0)return $item;}, $data);$res = array_filter($arr, function($item){return $item;});//过滤值$res = array_values($res);//重新排序数组printf('<pre>%s</ pre>', print_r($res,true));
//输出:// [2] => 2// [4] => 4// …//[100] => 100
相关推荐
© 2020 asciim码
人生就是一场修行