Blogger Information
Blog 12
fans 0
comment 1
visits 5846
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
7.30作业函数用法
小陈先生的博客
Original
805 people have browsed it

<?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);

Correction status:qualified

Teacher's comments:还是补之前的作业呢, 建议之前的作业先放放, 尽可能跟上进度, 否则二头都顾不上, 之前的作业 , 可以放在双休日集中完成
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post