批改状态:合格
老师批语:这些都是最重要的内容,希望记住并尽可能去使用它们
函数是实现代码复用的重要方式,在所有编程语言中均如此
function 函数名称(类型: 参数列表): 返回值类型{// 函数体return 返回值;}

| 序号 | 名称 | 描述 |
|---|---|---|
| 1 | function |
声明函数 |
| 2 | 函数名称 |
符合 PHP 标识符命名规范,不区分大小写 |
| 2 | 参数列表 |
零个或多个接收外部传入到函数的变量 |
| 2 | {... |
创建出一个封闭的函数作用域 |
| 2 | 函数体 |
由零个可多个合法的 PHP 语句组成 |
| 2 | return 值 |
将执行结果返回函数调用者[可选] |
| 2 | ...} |
函数执行结束,如果没有return,则返回null |
| 序号 | 类型 | 语法 | 描述 |
|---|---|---|---|
| 1 | 自定义函数 | function getName(){...} |
用户根据业务需求创建 |
| 2 | 系统函数 | substr(), count()... |
也叫预定义函数,不必声明直接调用 |
| 3 | 可变函数 | $funcName(); |
函数名使用变量表示 |
| 4 | 匿名函数 | $f = function (){...} |
也叫”闭包”或”函数表达式”,常用做回调处理 |
<?php// 函数// 1. 自定义函数// 2. 系统函数// 3. 可变函数// 4. 匿名函数:// getPrice: 小驼峰命名法// get_price: 蛇形命名法function getPrice(float $a, float $b) : float{return $a * $b;}echo '实际数量:'.getPrice(200,300);echo '<hr>';// 2.系统函数$str = '中国广东惠州';// 获取个字符echo mb_substr($str,0,4);echo '<hr>';// 3.可变函数$funcName = 'getPrice';echo '实际数量:'.$funcName(8000,0.8);echo '<hr>';// 匿名函数:闭包,可以访问父作用域的变量$discount = 0.8;$getAmont= function(float $money,int $num) : float{global $discount;$amount = $money * $num;return $amount >= 2000 ?$amount*$discount :$amount;};echo '实际:'.$getAmont(100,20);echo '<hr>';$getAmont = function(float $money,int $num)use ($discount) :float{$amount = $money * $num;return $amount >= 2000 ?$amount*$discount :$amount;};echo '实际:'.$getAmont(1000,20);echo '<hr>';// 如果父作用域又是一个函数$f = function ($discount){$getAmont=function (float $money,int $num) use ($discount): float{$amount = $money * $num ;return $amount >= 2000 ? $amount * $discount : $amount;};return $getAmont;};echo '实际:'.$f(0.6)(1000,20);// echo '<hr>';// $stn=$f(0.6);// echo $stn(1000,20);

| 序号 | 场景 | 描述 |
|---|---|---|
| 1 | return |
可以返回任何类型的值,包括函数类型 |
| 2 | 无return |
遇到}也会返回, 默认返回null |
| 序号 | 返回值类型 | 描述 |
|---|---|---|
| 1 | string |
字符串拼接 |
| 2 | array |
数组 |
| 3 | json |
JSON 字符串 |
| 4 | serialize |
序列化字符串 |
json 和序列化,使用时需要进行解码操作
<?php// 返回值// 原则: 单值返回// 如果需要返回多值// 1. 字符串拼装// 2. 数组// 3. JSON字符串// 4. 序列化: 字符串// 1. 字符串拼装function demo1() : string{$status = 1;$message = '成功';return $status .':'.$message;}echo demo1();// 适合处理大量的php,heml混写// 2.通过数组function demo2() : array{return ['strtus'=>1,'message'=>'成功'];}echo print_r(demo2(),true);echo '<pre>'.print_r(demo2(),true).'</pre>';printf('<pre>%s</pre>',print_r(demo2(),true));// echo demo2()['status'] == 1 ? '<span style="color:green">'.demo2()['message']. '</span>' : '验证失败';echo demo2()['strtus'] == 1 ? '<span style="color:green">'.demo2()['message']. '</span>' : '验证失败';// 3. 通过JSON返回// json: 是用js对象字面量的方式来表示数据,是一种轻量级通用的数据交换或传输格式// json本质上就是一个具有一定结构和格式的字符串, 不过这种格式得到了公认,几乎所有编程语言都有支持它的接口echo '<hr>';function demo3():string{return json_encode(['stratus'=>2,'message'=>'失败']);}// echo demo3();$data =demo3();echo $data;$var=json_decode($data,true);print_r($var);echo '<hr>';function demo4() :string{// 序列化 serializereturn serialize(['stratus'=>1,'message'=>'成功']);}echo demo4();// 反序列化 unserialize$arr = unserialize(demo4());printf('<pre>%s</pre>',print_r($arr,true));

参数类型
| 序号 | 类型 | 描述 |
|---|---|---|
| 1 | 值参数 | 默认传参方式 |
| 2 | 引用参数 | 改变原始调用参数值 |
| 3 | 默认参数 | 调用时允许省略的参数 |
| 4 | 剩余参数 | 调用参数数量不确定 |
<?php// 函数参数// 1. 值参数// 2. 引用参数// 3. 默认参数// 4. 剩余参数// 1.值参数,默认function demo1(float $arg) : float{$arg = $arg * 2;return $arg;}$value = 100;echo demo1($value),'<br>';// 在函数中改变了调用参数的值,但原始调用参数并没有发生变化echo $value;echo '<hr>';function demo2(float &$arg) :float{$arg = $arg * 2;return $arg;}$value = 100;echo demo2($value),'<br>';// 如果在参数前面使用了取地址符,则会改变原始调用参数的值echo $value;echo '<hr>';// 3.默认参数// 默认参数必须写在必选参数的后面function demo3(float $a ,float $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;case '*':$res = "$a * $b = " . ($a *$b);break;default:$res = "非法操作";}return $res;}echo demo3(20,20).'<br>';echo demo3(20,20,'/').'<br>';echo demo3(20,20,'#').'<br>';echo '<hr>';// 4.剩余参数function demo4(float $a ,float $b,float $c){return $a + $b+$c;}echo demo4(1,2,3).'<br>';echo '<hr>';function demo5(){// 参数数量// return func_num_args();// return func_get_arg(5);// return func_get_args();$total=0;// for($i=0;$i <func_num_args();$i++){// $total += func_get_arg($i);// }// return $total;foreach(func_get_args() as $value){$total += $value;}return $total;}print_r(demo5(1,2,3,6,7,64,2463)).'<br>';echo '<hr>';function demo6(...$args) :float{// return $args;// return array_sum($args);return array_product($args);}print_r(demo6(1,2,3,6,7,64,2463)).'<br>';echo '<hr>';// ...:// 1. 用在函数的形式参数列表中,表示"收集",将多个离散的参数打包到一个数组中处理// 2. 用在函数的调用参数列表中,表示"展开",还原将一个数组展开成一个个离散的值$arr=[1,2,3,6,7,64,2463];print_r(demo6(...$arr));echo '<hr>';$str[] = [100,'php','html'];$str[] = [100,'php','html'];$str[] = [100,'php','html'];foreach ($str as list($id,$name)){printf('id=%s, name=%s<br>', $id, $name);}
-
| 语法 | 类型 | 执行方式 | 应用场景 |
|---|---|---|---|
| 匿名函数 | 闭包Closure |
异步 | 函数参数 |
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号