Blogger Information
Blog 8
fans 0
comment 3
visits 8179
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
详细列举函数的4种类型、4种参数以及4种返回值的用法
程东旭
Original
2835 people have browsed it

4月20日作业:

1. 举例演示函数的四种类型

  1. //1.自定义函数
  2. function userName():string
  3. {
  4. return '程东旭';
  5. }
  6. echo userName();
  7. //2.系统函数
  8. echo '<hr>';
  9. function aaa():int
  10. {
  11. return time();
  12. }
  13. echo aaa();
  14. //3.可变函数
  15. echo '<hr>';
  16. $type = 'email';
  17. function email():string
  18. {
  19. return 'admin@php.cn';
  20. }
  21. echo $type();
  22. //4.匿名函数,闭包
  23. echo '<hr>';
  24. $max = function (int $a, int $b):int
  25. {
  26. return max($a, $b);
  27. };
  28. echo $max(10, 20);

2. 举例演示函数多值返回的四种形式

  1. //1.返回值是字符串
  2. echo '<hr>';
  3. function user():string
  4. {
  5. $name = 'admin';
  6. $email = 'admin@php.cn';
  7. return $name . '的邮箱是' . $email;
  8. }
  9. echo user();
  10. //2.返回值是数组
  11. echo '<hr>';
  12. function user1():array
  13. {
  14. return ['程东旭', 30, 'chengdongxu@php.cn'];
  15. }
  16. $user1 = print_r(user1(), true);
  17. printf('<pre>%s</pre>', $user1);
  18. //3.返回值是json
  19. echo '<hr>';
  20. function user2():string
  21. {
  22. return json_encode(['name' => 'chengdongxu', 'age' => 30, 'email' => 'chengdongxu@php.cn']);
  23. }
  24. $user2 = user2();
  25. echo $user2;
  26. //4.返回值是序列化
  27. echo '<hr>';
  28. function phone():string
  29. {
  30. return serialize(['name' => 'apple phone', 'price' => 8000, 'color' => 'red']);
  31. }
  32. $phone1 = phone();
  33. echo $phone1;
  34. echo '<hr>';
  35. //反序列化
  36. $phone2 = unserialize(phone());
  37. printf('<pre>%s</pre>', print_r($phone2, true));

3. 举例演示函数的四种参数

  1. //1.值参数,仅传递值
  2. $age2 = 30;
  3. echo '<hr>';
  4. function user4(int $age):int
  5. {
  6. return $age += 2;
  7. }
  8. echo '我现在的年龄是' . user4($age2) . '<br>';
  9. echo '我2年前的年龄是' . $age2 . '<br>';
  10. //2.引用传递
  11. echo '<hr>';
  12. function user5(int &$age2):int
  13. {
  14. return $age2 += 5;
  15. }
  16. $age3 = 30;
  17. echo '我现在年龄是' . user5($age3) . '<br>';
  18. echo '我现在年龄是' . $age3 . '<br>';
  19. //3.默认参数
  20. echo '<hr>';
  21. function user6(int $a, int $b, string $opt = '*')
  22. {
  23. $res = 0;
  24. switch ($opt) {
  25. case '+':
  26. $res = "$a + $b = " . ($a + $b);
  27. break;
  28. case '-':
  29. $res = "$a - $b = " . ($a - $b);
  30. break;
  31. case '*':
  32. $res = "$a * $b = " . ($a * $b);
  33. break;
  34. default:
  35. $res = '非法操作';
  36. }
  37. return $res;
  38. }
  39. echo user6(300, 200);
  40. echo user6(300, 200, '+');
  41. //4.剩余参数
  42. echo '<hr>';
  43. function user7()
  44. {
  45. $num = 0;
  46. foreach (func_get_args() as $value){
  47. $num += $value;
  48. }
  49. return $num;
  50. }
  51. print_r(user7(1,5,55,66,33255,55,445,1));

4月20日课程总结

1、函数的4种类型分别是:自定义函数、系统函数、可变函数、匿名函数;

2、函数的4种不同的返回值类型:字符串、数据、JSON、序列化字符串;

3、函数的4种参数类型:值参数、引用参数、默认参数、剩余参数。

自我感受

函数的4种类型和4种返回值都比较好理解,4种参数里面的值参数和引用参数也不难,默认参数和剩余参数有些糊涂;

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

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