批改状态:合格
老师批语:
// 只会返回一个return 且第一个return后面代码不会执行function demo(){return sha1('wwwphpxnccsdsds');return md5('wwwphpcn');return 1==1;return new StdClass;return array('12','45');return 88.88;return 'nihao';return true;}echo '<hr>';var_dump(demo());// 多值返回// 数组function arr(){return ['status'=>1,'msg'=>'登录成功'];}echo '<hr>';print_r(arr());// 对象function demo2(){return new class(){public $name = 'chloe';public $gender = '女';};}$obj = demo2();var_dump($obj);echo '<hr>';echo $obj->name;echo $obj->gender;echo '<hr>';// 参数function mx($name){echo $name;}mx('刘星');// 封装函数// 编写代码时 将需要随时更改的变量 由函数的参数进行传入 方便代码的调试和修改// 函数的参数可以设置默认的参数 建议 默认参数设置在最右边function createTable(int $rows,int $cols,string $content,string $bgColor=blue) :string{$table = "<table border ='1' cellspacing='0' bgColor='$bgColor'>";for ($i=0; $i < $rows; $i++) {$table .= "<tr>";for ($j=0; $j < $cols ; $j++) {$table .= "<td>$content</td>";}$table .= "</tr>";}$table .= '</table>';return $table;}echo createTable(4,3,'你好','red');// 匿名函数// 临时创建的一个没有名字的函数// 命名函数可以全局识别 匿名函数不能$closure = function($name){return "欢迎{$name}来到php中文网做客<br>";};echo $closure('胡歌');// 变量作用域// 函数外部变量可以通过参数形式传入内部// 全局变量如果想在自定义函数内部使用则必须使用 global /$GLOBALS关键字声明// 自定义函数内部变量在退出声明变量的函数时,变量和值会被清除$one = 200;$two = 300;$itemName = 'iphone 12 pro max 256g';$itemPrice = 12585;function add(int $a,int $b):string{return $a+$b;}// var_dump(add(1,2));var_dump(add($one,$two));function getItemInfo_pro(){// global $itemPrice,$itemName;return sprintf('今日主推产品:%s, 商品价格: %d<br>',$GLOBALS['itemName'],$GLOBALS['itemPrice']);}echo getItemInfo_pro();
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号