批改状态:合格
老师批语:
// 声明在函数外部的成员,默认就是全局成员$laos = '后端老师';$youx = '0121223154@qq.com';function index1(): string{// 函数作用域: 只有调用它的时候才会创建$dsw= '';// 1. global 访问全局/外部的成员global $laos;$dsw .= 'laos = ' . $laos . '<br>';// 2. $GLOBALS 超全局数组$dsw .= 'youx = ' . $GLOBALS['youx'] . '<br>';return $dsw;}echo index1(),'<hr>';// 2. 闭包: 匿名函数 闭包: 可以访问函数外部的自由变量/父作用域中的变量$bbao2 = function () use ($laos, $youx) {return sprintf('laos = %s<br>youx = %s<br>', $laos, $youx);};echo $bbao2(),'<hr>';// 闭包支持引用传参: 参数前加&echo '当前: laos = ' . $laos . '<br>';$gb3 = function ($mylaos) use (&$laos) {// 闭包中将引用参数更新后,会实时的映射到外部的原始条件中$laos = $mylaos;};$gb3('前端老师');echo '现在: laos = ' . $laos . '<br>';// 闭包经常用作函数的返回值function bbhs4($site){// 返回一组函数,闭包最佳使用场景return function ($color) use ($site) {return sprintf('<h3 style="color:%s">%s</h3>', $color,$site);};}// 高阶函数: 柯里化echo bbhs4('提交作业完成')('violet');

php单选程,同步执行,如果遇到耗时函数会发生阻塞,应该将它改为异步回调执行
// 回调方法function index1(string $name) : string{return "Hello waayou $name !";}// 传统echo index1('早上好'), '<hr>';// call_user_func(函数, 参数列表)echo call_user_func('index1','中午好'), '<hr>';// call_user_func_array(函数, [参数数组])echo call_user_func_array('index1',['晚上好']), '<hr>';// 这二个函数还可以异步的调用对象方法class User{// 实例方法public function hello(string $name) : string{return "Hello waayou $name !";}// 静态方法: 类调用public static function say(string $site) : string{return "上班要打卡 $site !";}}// 实例方法$xqh = call_user_func_array([new User, 'hello'], ['编程老师']);echo $xqh,'<hr>';// 静态方法$xqh = call_user_func_array('User::say', ['不能早退']);echo $xqh, '<br>';

<?php// 1. 数组function index1(): array{return ['zsygzy' => 11, 'tijiaozy' => '提交成功'];}$szy = index1();echo $szy['zsygzy'] === 1 ? $szy['tijiaozy'] : '提交失败.....';echo '<hr>';// 2. 对象function index2() : object{return new class (){public $email = '邮箱';public $passow = '密码';};}$user = index2();printf('email = %s<br>passow = %s<br>', $user->email, $user->passow);echo '<hr>';// 3. 序列化的字符串function index3(): string{return serialize(['email' => 1, 'passow' => '验证通过']);}$xlh = index3();echo $xlh,'<hr>';// 在php中使用时要还原成原来的类型$arr = unserialize($xlh);echo var_dump($arr),'<hr>';// 4. 转为通用的json格式的字符串function index4(): string{return json_encode(['email' => 2, 'passow' => '验证通过'], JSON_UNESCAPED_UNICODE);}$xlh = index4();$str = json_decode($xlh);echo var_dump($str),'<hr>';// 默认将外部的json解析成object对象类型printf('email = %s<br>passow = %s<hr>',$user->email, $user->passow);

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号