<?php
/**
* Created by PHPSTORM.
* User: ChenGuo
* Date: 2019/7/31
* Time: 20:14
*/
//传统参数 计算两个数之间的和
echo "传统参数 计算两个数之间的和"."<br>";
function count_sum($a,$b){
if($a>=$b){
echo "亲,这样是不对的";
die;
}
$sum = 0;
for($i = $a;$i<=$b;$i++){
$sum += $i;
}
return $sum;
}
echo count_sum(1,100);
echo "<hr>";
echo "演示剩余参数"."<br>";
//剩余参数
function demo($a,$b,...$c){
//用法1 fun_get_args获取所有参数
$sum = 0;
foreach ( func_get_args() as $value){
$sum += $value;
}
return $sum;
//用法2 array_sum直接求和
// return $a+$b+array_sum($c);
}
$c=[1,2,3,4];
echo demo(1,2,...$c);
echo "<hr>";
echo "演示利用匿名函数改变外部数据"."<br>";
$a = '这是外部数据';
$b = function () use(&$a){
$a = '$a=这是被改变的数据';
return $a;
};
echo $b();
echo "<hr>";
echo "演示call_user_func_array用法"."<br>";
class aaa{
public static function sum($a,$b){
return $a + $b;
}
}
$a = [1,2];
echo call_user_func_array(['aaa','sum'],$a);