Blogger Information
Blog 18
fans 0
comment 0
visits 11025
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
210428 PHP 函数 返回值 参数 闭包/匿名函数
xyphpblog
Original
825 people have browsed it

1. 函数

function 函数名(参数类型:可选,参数):返回值类型限定
{
函数体
return 返回值(返回任意类型,单个值,之后代码不会执行);
}

  1. //无返回值
  2. function f1()
  3. {
  4. return md5("abc");
  5. }
  6. echo f1(), "<hr>";

2. 函数返回值

2.1 返回指定类型

  1. //返回指定类型
  2. function f2($a, $b): int
  3. {
  4. return $a + $b;
  5. }
  6. echo f2(1.1, 2.2), "<hr>"; //3

2.2 返回数组

  1. //返回数组
  2. function f3(): array
  3. {
  4. return ['name' => 'Em', 'age' => 18];
  5. }
  6. $arr = f3();
  7. print_r($arr); //Array ( [name] => Em [age] => 18 )
  8. echo '<hr>';

2.3 返回对象

  1. //返回对象 以匿名类返回
  2. function f4(): object
  3. {
  4. return new class
  5. {
  6. public $id = '0001';
  7. public $gender = 'male';
  8. };
  9. }
  10. $user = f4();
  11. var_dump($user);
  12. //object(class@anonymous)#1 (2) { ["id"]=> string(4) "0001" ["gender"]=> string(4) "male" }
  13. //访问对象属性
  14. echo $user->gender; //male
  15. echo '<hr>';

2.4 返回JSON字符串

  1. //返回JSON字符串
  2. function f5(): string
  3. {
  4. global $arr;
  5. return json_encode($arr);
  6. }
  7. $jsonStr = f5();
  8. echo $jsonStr; // {"name":"Em","age":18}
  9. //解析JSON字符串,还原成PHP可以处理的数据类型
  10. $jd = json_decode($jsonStr);
  11. var_dump($jd);
  12. //object(stdClass)#2 (2) { ["name"]=> string(2) "Em" ["age"]=> int(18) }
  13. //JSON字符串解析为数组
  14. $jd = json_decode($jsonStr, true);
  15. var_dump($jd);
  16. //array(2) { ["name"]=> string(2) "Em" ["age"]=> int(18) }

2.5 序列化字符串

  1. //返回 序列化字符串
  2. function f6()
  3. {
  4. global $arr;
  5. return serialize($arr);
  6. }
  7. echo f6();
  8. //a:2:{s:4:"name";s:2:"Em";s:3:"age";i:18;}
  9. file_put_contents('f6.txt', f6());
  10. //反序列化
  11. var_dump(unserialize(f6()));
  12. //array(2) { ["name"]=> string(2) "Em" ["age"]=> int(18) }

3. 参数

3.1 普通带参数函数

  1. # 循环创建表格
  2. function createTable(int $row, int $col)
  3. {
  4. $table = "<table border='1' cellspacing='0'>";
  5. for ($i = 0; $i < $row; $i++) {
  6. $table .= "<tr>";
  7. for ($j = 0; $j < $col; $j++) {
  8. $table .= "<td>1</td>";
  9. }
  10. $table .= "</tr>";
  11. }
  12. $table .= "</table>";
  13. return $table;
  14. }
  15. echo createTable(2, 3), '<hr>';

3.2 参数有默认值

  1. // 参数有默认值
  2. function createTable2(int $row = 2, int $col = 2)
  3. {
  4. $table = "<table border='1' cellspacing='0'>";
  5. for ($i = 0; $i < $row; $i++) {
  6. $table .= "<tr>";
  7. for ($j = 0; $j < $col; $j++) {
  8. $table .= "<td>1</td>";
  9. }
  10. $table .= "</tr>";
  11. }
  12. $table .= "</table>";
  13. return $table;
  14. }
  15. echo createTable2(), '<hr>';

3.3 参数部分有默认值,部分没有默认值

  1. // 参数部分有默认值,部分没有默认值
  2. function createTable3(int $row, int $col, string $bgcolor = "lightblue", string $text = '?')
  3. {
  4. $table = "<table border='1' cellspacing='0' bgcolor={$bgcolor}>";
  5. for ($i = 0; $i < $row; $i++) {
  6. $table .= "<tr>";
  7. for ($j = 0; $j < $col; $j++) {
  8. $table .= "<td>{$text}</td>";
  9. }
  10. $table .= "</tr>";
  11. }
  12. $table .= "</table>";
  13. return $table;
  14. }
  15. echo createTable3(5, 6), '<hr>';

3.4 引用参数

  1. // 引用参数
  2. function fo(&$p)
  3. {
  4. return $p .= $p;
  5. }
  6. $v1 = '?';
  7. echo fo($v1), '<br>';
  8. echo $v1, '<hr>';

3.5 剩余参数

  1. # 剩余参数
  2. function getSum(...$params)
  3. {
  4. return array_sum($params);
  5. }
  6. $arr = [1, 2, 3, 4, 5];
  7. echo getSum(...$arr), '<hr>'; //15
  8. echo getSum(1, 2, 3); //6

4. 闭包/匿名函数

  1. <?php
  2. # 匿名函数 闭包
  3. $closure = function ($name) {
  4. return 'name: ' . $name;
  5. };
  6. echo $closure('Em'), '<br>'; // name: Em
  7. # 闭包通过use 可以访问函数外部的自由变量/父作用域中的变量
  8. $name = 'Em';
  9. $age = 22;
  10. $closure2 = function () use ($name, $age) {
  11. return sprintf('name: %s <br> age: %d', $name, $age);
  12. };
  13. echo $closure2(), '<br>';
  14. //name: Em
  15. //age: 22
  16. #闭包通过use引用传递
  17. $closure3 = function ($newAge) use (&$age) {
  18. $age = $newAge;
  19. };
  20. $closure3(18);
  21. echo $age, '<br>'; //18
  22. #闭包常用作函数返回值
  23. function foo($animal)
  24. {
  25. return function ($sound) use ($animal) {
  26. return sprintf('%s: %s', $animal, $sound);
  27. };
  28. }
  29. echo foo('?')('meow~');

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