批改状态:合格
老师批语:第一次听说是有点难, 多做几个练习就好了
//1.自定义函数function userName():string{return '程东旭';}echo userName();//2.系统函数echo '<hr>';function aaa():int{return time();}echo aaa();//3.可变函数echo '<hr>';$type = 'email';function email():string{return 'admin@php.cn';}echo $type();//4.匿名函数,闭包echo '<hr>';$max = function (int $a, int $b):int{return max($a, $b);};echo $max(10, 20);
//1.返回值是字符串echo '<hr>';function user():string{$name = 'admin';$email = 'admin@php.cn';return $name . '的邮箱是' . $email;}echo user();//2.返回值是数组echo '<hr>';function user1():array{return ['程东旭', 30, 'chengdongxu@php.cn'];}$user1 = print_r(user1(), true);printf('<pre>%s</pre>', $user1);//3.返回值是jsonecho '<hr>';function user2():string{return json_encode(['name' => 'chengdongxu', 'age' => 30, 'email' => 'chengdongxu@php.cn']);}$user2 = user2();echo $user2;//4.返回值是序列化echo '<hr>';function phone():string{return serialize(['name' => 'apple phone', 'price' => 8000, 'color' => 'red']);}$phone1 = phone();echo $phone1;echo '<hr>';//反序列化$phone2 = unserialize(phone());printf('<pre>%s</pre>', print_r($phone2, true));
//1.值参数,仅传递值$age2 = 30;echo '<hr>';function user4(int $age):int{return $age += 2;}echo '我现在的年龄是' . user4($age2) . '<br>';echo '我2年前的年龄是' . $age2 . '<br>';//2.引用传递echo '<hr>';function user5(int &$age2):int{return $age2 += 5;}$age3 = 30;echo '我现在年龄是' . user5($age3) . '<br>';echo '我现在年龄是' . $age3 . '<br>';//3.默认参数echo '<hr>';function user6(int $a, int $b, string $opt = '*'){$res = 0;switch ($opt) {case '+':$res = "$a + $b = " . ($a + $b);break;case '-':$res = "$a - $b = " . ($a - $b);break;case '*':$res = "$a * $b = " . ($a * $b);break;default:$res = '非法操作';}return $res;}echo user6(300, 200);echo user6(300, 200, '+');//4.剩余参数echo '<hr>';function user7(){$num = 0;foreach (func_get_args() as $value){$num += $value;}return $num;}print_r(user7(1,5,55,66,33255,55,445,1));
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号