批改状态:合格
老师批语:
1.函数使用return返回单个值
2.多个值的返回,可以用数组的形式返回
3.函数可以返回object对象。
4.函数可以返回json字符串。
json_encode(字符串,第二参数)
json字符串参数 256中文不转义
json字符串 64不转义/
可以叠加使用 256+64
5.以序列化字符串返回,常用于记录日志
1.引用参数
引用参数,在函数里面发生变化,都会对父级产生影响。
2.剩余参数
包含 … 的参数,会转换为指定参数变量的一个数组
<?phpfunction sum(...$numbers) {$acc = 0;foreach ($numbers as $n) {$acc += $n;}return $acc;}echo sum(1, 2, 3, 4);?>
3.默认参数
<?phpfunction makeyogurt($flavour, $type = "acidophilus"){return "Making a bowl of $type $flavour.\n";}echo makeyogurt("raspberry"); // works as expected?>
匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数。最经常用作回调函数 callable参数的值。当然,也有其它应用的情况。
匿名函数目前是通过 Closure 类来实现的。
<?php$greet = function($name){printf("Hello %s\r\n", $name);};$greet('World');$greet('PHP');?>
闭包可以从父作用域中继承变量。
<?php$message = 'hello';// 没有 "use"$example = function () {var_dump($message);};$example();// 继承 $message$example = function () use ($message) {var_dump($message);};$example();// Inherited variable's value is from when the function// is defined, not when called$message = 'world';$example();// Reset message$message = 'hello';// Inherit by-reference$example = function () use (&$message) {var_dump($message);};$example();// The changed value in the parent scope// is reflected inside the function call$message = 'world';$example();// Closures can also accept regular arguments$example = function ($arg) use ($message) {var_dump($arg . ' ' . $message);};$example("hello");?>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号