批改状态:合格
老师批语:
1.语法
function 函数名称([参数类型限定 参数列表]):返回值类型的限定{return 返回值;//注意函数体只能返回单个的值,返回值的类型是任意的类型的。echo 'hello';//不会执行因为执行了return。}function text(int $a,float $b):int{return $a+$b;}echo($res=text(12, 12.3));//24function text(int $a,float $b):int{return $a+$b;echo 'helloworld';//不会执行}echo($res=text(12, 12.3));//24//数组返回function demo1():array{return ['status'=>1,'msg'=>'验证成功'];}$res=demo1();echo $res['status']===1?$res['msg']:'验证失败';//对象返回function demo2():object{//匿名类return new class(){public $name="admin";public $email="12323@qq.com";};}$user=demo2();var_dump($user);//对象成员的访问 ->echo $user->name;echo $user->email;//转为json格式的字符串返回function demo3():string{return json_encode(['status'=>1,''sg'=>'验证成功'],JSON_UNESCAPEO_UNICODE);}$json_str=demo3();echo $json_str;//{"status":1,"msg":"验证成功"}//需要将json字符串解析,json_decode()还原成php能够处理的数据类型$res=json_decode($json_str,true);var_dump($res);//json object//也可以使用数组方式给定第二个参数$res=json_decode($json_str,true);var_dump($res);//array//以序列化字符串返回function demo4(){return serialize(['status'=>1,'msg'=>'登陆成功','code'=>254]);}$res=demo4();var_dump($res);
//循环实现表格
$table="<table border='1' cellspacing='0'>";for($i=0;$i<3;$i++){//外层循环控制表格的行$table.="<tr>";//内层循环控制表格的列for($j=0; $j<2; $j++){$table .="</td>中国</td>";}$table .="</tr>";}$table.="</table>";echo $table;
//函数的参数多个参数用逗号隔开function createTable(int $rows,int $colunms){$table="<table border='1' cellspacing='0'>";for($i=0;$i<$rows;$i++){//外层循环控制表格的行$table.="<tr>";//内层循环控制表格的列for($j=0; $j<$colunms; $j++){$table .="</td>中国</td>";}$table .="</tr>";}$table.="</table>";return $table;}echo createTable(2,3);
//默认传入的参数$bgColor="pink",$content="我".function createTable(int $rows,int $colunms,string $bgColor="pink",string $content="我"){$table="<table border='1' cellspacing='0' bgColor='{$bgColor}'>";for($i=0;$i<$rows;$i++){//外层循环控制表格的行$table.="<tr>";//内层循环控制表格的列for($j=0; $j<$colunms; $j++){$table .="</td>{$content}</td>";}$table .="</tr>";}$table.="</table>";return $table;}echo//用户相应传入的参数。createTable(3,4,'red','发');
//引用参数function ref(&$arg){return $arg*=2;}$val=20;echo ref($val);//40echo $val;//40//结论:把$val的内存储存区块相对地址导入到函数中了,在函数里面发生的任何变化,都会对父程序造成影响。
//剩余参数适用于参数不固定//剩余参数用在参数列表中合并function add($a,$b,$c,$d){return $a+$b+$c+$;}add(1,2,3,6);//剩余参数用在函数的调用表达式中展开function test(...$arg){//array_sum()计算数组的和return array_sum($arg);$arr=[1,2,3,4,5];echo test(...$arr);//15
//命名函数echo demo("小明");//小明function demo($name){return $name;}echo demo("小明");//小明//匿名函数echo $func("小红");匿名函数不能提前调用$func=function(){echo $name;}echo $func("小红");//小红//匿名函数通过use可以访问函数外部的自由变量。$userName="admin";$email="1232@qq.com";$text=function()use($userName,$email){return sprintf($userName,$email);};//匿名函数的调用echo $text();//admin,1232@qq.com$text=function($myName,$myEmail)use($userName,$email){$name=$myName;$email=$myEmail;};$text('yin','jjkkl@qq.com');echo $userName//yin
//回调函数:将函数作为函数返回值调用。function demo1($site){return function($color)use($site){return function (){return spintf('<h1 style="color:red;"></h1>',$color,$site);};};}//调用demo1$res=demo1("php.cn");//调用demo1里面的参数echo $res('red');
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号