Blogger Information
Blog 52
fans 1
comment 1
visits 38281
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
总结函数的返回值,参数
小丑0o鱼
Original
542 people have browsed it
  1. 函数定义
  2. function 函数名(参数1,参数2,…){
  3. //函数体
  4. //return
  5. }
  6. 返回值
  7. PHP 中提供了 return 语句来返回函数的运行结果,其语法格式如下: return 返回值; 可以返回包括数组和对象的任意类型。返回语句会立即中止函数的运行,并且将控制权交回调用该函数的代码行。
  8. 返回单个值
  9. function square($num)
  10. {
  11. return $num * 2;
  12. }
  13. echo square(2),'<br>'; //输出 4.
  14. 函数不能返回多个值,但可以通过返回一个数组来得到类似的效果
  15. // 返回数组
  16. function numbers()
  17. {
  18. return array(1, 2, 3);
  19. }
  20. var_dump(numbers()); //输出 array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }.
  21. 返回json字符串
  22. // 返回json字符串
  23. function jsons()
  24. {
  25. return json_encode(array('code'=>1,'msg'=>'登陆成功'),JSON_UNESCAPED_UNICODE);
  26. }
  27. var_dump(jsons()); //输出 string(31) "{"code":1,"msg":"登陆成功"}".
  28. 返回序列化
  29. function selizes()
  30. {
  31. return serialize(array('code'=>1,'msg'=>'登陆成功'));
  32. }
  33. var_dump(selizes()); //输出 string(51) "a:2:{s:4:"code";i:1;s:3:"msg";s:12:"登陆成功";}".
  34. 返回函数
  35. function funs()
  36. {
  37. return function (){};
  38. }
  39. var_dump(funs()); //输出 object(Closure)#1 (0) { }.
  40. 参数
  41. 函数的参数有形式参数和实际参数
  42. 1、函数的参数默认是值传递
  43. 2、如果要传递地址,在参数前面加&
  44. 3、如果是地址传递,不能直接写值
  45. 参数个数不匹配
  46. 1、实参少于形参(报错)
  47. 2、实参多于形参,只取前面对应的值
  48. 3func_num_args() //获取参数的个数
  49. 4func_get_args() //获取参数数组
  50. 参数默认值
  51. 1、在定义函数的时候给形参赋值就是参数的默认值
  52. 2、默认值必须是值,不能用变量代替
  53. 3、默认值可以使用常量
  54. 4、有默认值的写在后面,没有默认值的写在前面
  55. function sum($a, $b, $c=10)
  56. {
  57. return $a + $b + $c;
  58. }
  59. echo sum(1,2,20); // 输出 23
  60. echo sum(1, 2); //输出 13 c使用了默认值10
  61. echo sum(1, 2, 30, 40); //输出 33 不报错
  62. 定义变长参数...$参数
  63. 当使用变长参数时,可使用…$arry展开数组
  64. function sum(...$numbers) {
  65. $acc = 0;
  66. foreach ($numbers as $n) {
  67. $acc += $n;
  68. }
  69. return $acc;
  70. }
  71. echo sum(1, 2, 3, 4); //输出 10
  72. 参数约束(7.0后)
  73. 类型约束
  74. //约束$name是字符串型,$age是整型
  75. function fun(string $name,int $age) {
  76. echo "姓名:{$name},年龄:{$age}";
  77. }
  78. fun('tom',22); //输出姓名:tom,年龄:22
  79. 返回值约束
  80. function fun(int $num1,int $num2):int { //必须返回整型
  81. return $num1+$num2;
  82. }
  83. echo fun(10,20); //输出 30
  84. 2. 回调函数,匿名函数
  85. 回调函数
  86. 回调函数就是一个作为函数的参数来传递的函数. 回调函数在主线程执行的过程中,突然跳去执行设置的回调函数,回调函数执行结束后, 再回到主线程处理接下来的流程
  87. php提供了两个内置函数call_user_func()和call_user_func_array()提供对回调函数的支持.
  88. call_user_func()call_user_func($callback,参数1,参数2,…)的参数个数根据回调函数的参数来确定的。
  89. // 自定义一个匿名函数
  90. $sum = function ($a, $b) {
  91. return $a + $b;
  92. };
  93. echo call_user_func($sum,2,4); //输出 6
  94. call_user_func_array()call_user_func_array ($callback ,$param_arr),它只有两个参数。是以数组的形式接收回调函数的参数
  95. // 自定义函数
  96. $sum = function (...$arr) {
  97. $total = 0;
  98. foreach ($arr as $v) {
  99. $total += $v;
  100. }
  101. return $total;
  102. };
  103. echo call_user_func_array($sum, array(1,2,3,4,5)); //输出 15
  104. 匿名函数
  105. 匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数。最经常用作回调函数 callable参数的值。
  106. // 匿名函数
  107. $greet = function($name)
  108. {
  109. printf("Hello %s\r\n", $name);
  110. };
  111. $greet('World'); //输出 Hello World
  112. $greet('PHP'); //输出 Hello PHP
  113. 匿名函数可以用 use 语言结构从父作用域中继承变量。
  114. $play = '吃过了么';
  115. $greet = function($name) use ($play)
  116. {
  117. printf("Hello %s,%s\r\n", $name, $play);
  118. };
  119. echo $greet('张三'); //输出 Hello 张三,吃过了么
Correction status:Uncorrected

Teacher's comments:
Statement of this Website
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!