Blogger Information
Blog 28
fans 0
comment 0
visits 25411
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php函数,函数语法、函数类型、函数返回值、函数参数
,多思曩惜,
Original
756 people have browsed it

函数

1. 语法

函数是实现代码复用的重要方式,在所有编程语言中均如此

  1. function 函数名称(类型: 参数列表): 返回值类型
  2. {
  3. // 函数体
  4. return 返回值;
  5. }
序号 名称 描述
1 function 声明函数
2 函数名称 符合 PHP 标识符命名规范,不区分大小写
2 参数列表 零个或多个接收外部传入到函数的变量
2 {... 创建出一个封闭的函数作用域
2 函数体 由零个可多个合法的 PHP 语句组成
2 return 值 将执行结果返回函数调用者[可选]
2 ...} 函数执行结束,如果没有return,则返回null

2. 类型

序号 类型 语法 描述
1 自定义函数 function getName(){...} 用户根据业务需求创建
2 系统函数 substr(), count()... 也叫预定义函数,不必声明直接调用
3 可变函数 $funcName(); 函数名使用变量表示
4 匿名函数 $f = function (){...} 也叫”闭包”或”函数表达式”,常用做回调处理

  1. <?php
  2. // 函数
  3. // 1. 自定义函数
  4. // 2. 系统函数
  5. // 3. 可变函数
  6. // 4. 匿名函数:
  7. // getPrice: 小驼峰命名法
  8. // get_price: 蛇形命名法
  9. function getPrice(float $a, float $b) : float{
  10. return $a * $b;
  11. }
  12. echo '实际数量:'.getPrice(200,300);
  13. echo '<hr>';
  14. // 2.系统函数
  15. $str = '中国广东惠州';
  16. // 获取个字符
  17. echo mb_substr($str,0,4);
  18. echo '<hr>';
  19. // 3.可变函数
  20. $funcName = 'getPrice';
  21. echo '实际数量:'.$funcName(8000,0.8);
  22. echo '<hr>';
  23. // 匿名函数:闭包,可以访问父作用域的变量
  24. $discount = 0.8;
  25. $getAmont= function(float $money,int $num) : float{
  26. global $discount;
  27. $amount = $money * $num;
  28. return $amount >= 2000 ?$amount*$discount :$amount;
  29. };
  30. echo '实际:'.$getAmont(100,20);
  31. echo '<hr>';
  32. $getAmont = function(float $money,int $num)use ($discount) :float
  33. {
  34. $amount = $money * $num;
  35. return $amount >= 2000 ?$amount*$discount :$amount;
  36. };
  37. echo '实际:'.$getAmont(1000,20);
  38. echo '<hr>';
  39. // 如果父作用域又是一个函数
  40. $f = function ($discount){
  41. $getAmont=function (float $money,int $num) use ($discount): float
  42. {
  43. $amount = $money * $num ;
  44. return $amount >= 2000 ? $amount * $discount : $amount;
  45. };
  46. return $getAmont;
  47. };
  48. echo '实际:'.$f(0.6)(1000,20);
  49. // echo '<hr>';
  50. // $stn=$f(0.6);
  51. // echo $stn(1000,20);

3. 返回值

  • 函数必须要有返回值
  • 函数必须是遵守单值返回原则
序号 场景 描述
1 return 可以返回任何类型的值,包括函数类型
2 return 遇到}也会返回, 默认返回null
  • 如果需要返回多个值,可以通过以下手段
序号 返回值类型 描述
1 string 字符串拼接
2 array 数组
3 json JSON 字符串
4 serialize 序列化字符串

json 和序列化,使用时需要进行解码操作

  1. <?php
  2. // 返回值
  3. // 原则: 单值返回
  4. // 如果需要返回多值
  5. // 1. 字符串拼装
  6. // 2. 数组
  7. // 3. JSON字符串
  8. // 4. 序列化: 字符串
  9. // 1. 字符串拼装
  10. function demo1() : string
  11. {
  12. $status = 1;
  13. $message = '成功';
  14. return $status .':'.$message;
  15. }
  16. echo demo1();
  17. // 适合处理大量的php,heml混写
  18. // 2.通过数组
  19. function demo2() : array
  20. {
  21. return ['strtus'=>1,'message'=>'成功'];
  22. }
  23. echo print_r(demo2(),true);
  24. echo '<pre>'.print_r(demo2(),true).'</pre>';
  25. printf('<pre>%s</pre>',print_r(demo2(),true));
  26. // echo demo2()['status'] == 1 ? '<span style="color:green">'.demo2()['message']. '</span>' : '验证失败';
  27. echo demo2()['strtus'] == 1 ? '<span style="color:green">'.demo2()['message']. '</span>' : '验证失败';
  28. // 3. 通过JSON返回
  29. // json: 是用js对象字面量的方式来表示数据,是一种轻量级通用的数据交换或传输格式
  30. // json本质上就是一个具有一定结构和格式的字符串, 不过这种格式得到了公认,几乎所有编程语言都有支持它的接口
  31. echo '<hr>';
  32. function demo3():string
  33. {
  34. return json_encode(['stratus'=>2,'message'=>'失败']);
  35. }
  36. // echo demo3();
  37. $data =demo3();
  38. echo $data;
  39. $var=json_decode($data,true);
  40. print_r($var);
  41. echo '<hr>';
  42. function demo4() :string
  43. {
  44. // 序列化 serialize
  45. return serialize(['stratus'=>1,'message'=>'成功']);
  46. }
  47. echo demo4();
  48. // 反序列化 unserialize
  49. $arr = unserialize(demo4());
  50. printf('<pre>%s</pre>',print_r($arr,true));

" class="reference-link">-

4. 参数

  • 调用者可以通过参数将数据传递到函数中
  • 参数是以逗号分隔的表达式列表
  • 参数按照从左到右的顺序求值

参数类型

序号 类型 描述
1 值参数 默认传参方式
2 引用参数 改变原始调用参数值
3 默认参数 调用时允许省略的参数
4 剩余参数 调用参数数量不确定

  1. <?php
  2. // 函数参数
  3. // 1. 值参数
  4. // 2. 引用参数
  5. // 3. 默认参数
  6. // 4. 剩余参数
  7. // 1.值参数,默认
  8. function demo1(float $arg) : float
  9. {
  10. $arg = $arg * 2;
  11. return $arg;
  12. }
  13. $value = 100;
  14. echo demo1($value),'<br>';
  15. // 在函数中改变了调用参数的值,但原始调用参数并没有发生变化
  16. echo $value;
  17. echo '<hr>';
  18. function demo2(float &$arg) :float
  19. {
  20. $arg = $arg * 2;
  21. return $arg;
  22. }
  23. $value = 100;
  24. echo demo2($value),'<br>';
  25. // 如果在参数前面使用了取地址符,则会改变原始调用参数的值
  26. echo $value;
  27. echo '<hr>';
  28. // 3.默认参数
  29. // 默认参数必须写在必选参数的后面
  30. function demo3(float $a ,float $b, string $opt = '+')
  31. {
  32. $res =0;
  33. switch($opt){
  34. case '+':
  35. $res = " $a + $b =" . ($a+$b);
  36. break;
  37. case '-':
  38. $res = "$a - $b =" . ($a-$b);
  39. break;
  40. case '/':
  41. $res = "$a / $b = " . ($a /$b);
  42. break;
  43. case '*':
  44. $res = "$a * $b = " . ($a *$b);
  45. break;
  46. default:
  47. $res = "非法操作";
  48. }
  49. return $res;
  50. }
  51. echo demo3(20,20).'<br>';
  52. echo demo3(20,20,'/').'<br>';
  53. echo demo3(20,20,'#').'<br>';
  54. echo '<hr>';
  55. // 4.剩余参数
  56. function demo4(float $a ,float $b,float $c)
  57. {
  58. return $a + $b+$c;
  59. }
  60. echo demo4(1,2,3).'<br>';
  61. echo '<hr>';
  62. function demo5()
  63. {
  64. // 参数数量
  65. // return func_num_args();
  66. // return func_get_arg(5);
  67. // return func_get_args();
  68. $total=0;
  69. // for($i=0;$i <func_num_args();$i++){
  70. // $total += func_get_arg($i);
  71. // }
  72. // return $total;
  73. foreach(func_get_args() as $value){
  74. $total += $value;
  75. }
  76. return $total;
  77. }
  78. print_r(demo5(1,2,3,6,7,64,2463)).'<br>';
  79. echo '<hr>';
  80. function demo6(...$args) :float
  81. {
  82. // return $args;
  83. // return array_sum($args);
  84. return array_product($args);
  85. }
  86. print_r(demo6(1,2,3,6,7,64,2463)).'<br>';
  87. echo '<hr>';
  88. // ...:
  89. // 1. 用在函数的形式参数列表中,表示"收集",将多个离散的参数打包到一个数组中处理
  90. // 2. 用在函数的调用参数列表中,表示"展开",还原将一个数组展开成一个个离散的值
  91. $arr=[1,2,3,6,7,64,2463];
  92. print_r(demo6(...$arr));
  93. echo '<hr>';
  94. $str[] = [100,'php','html'];
  95. $str[] = [100,'php','html'];
  96. $str[] = [100,'php','html'];
  97. foreach ($str as list($id,$name)){
  98. printf('id=%s, name=%s<br>', $id, $name);
  99. }

-

5. 回调函数

语法 类型 执行方式 应用场景
匿名函数 闭包Closure 异步 函数参数

> 异步执行,是指当前函数的执行并不会中断当前程序的执行流程

6. 命名空间

  • 使用目录来整理文档, 允许将同名文档,存储在不同的目录下面即可
  • 不同目录下的同名文件,访问时必须带上的它的目录名称,以未区别
  • 命名空间采用类似的思想,同名函数,只要声明在不同空间中即可
  • 同样, 访问这些函数时, 也需要带上它的命名空间才可以

总结

  • 了解PHP函数的语法,函数调用,四种函数的定义。
  • 了解返回值的原则,返回值的类型
  • 函数的参数,参数的调用。
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:这些都是最重要的内容,希望记住并尽可能去使用它们
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!