Blogger Information
Blog 38
fans 0
comment 0
visits 19153
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
变量与函数的声明,使用与参数实例演示
Blackeye
Original
365 people have browsed it

1

  1. <!-- 将变量与函数的声明,使用与参数实例演示 -->
  2. <?php
  3. // 变量
  4. $username="Dave";
  5. // 查看
  6. echo 'Hello '.$username . '<br/>';
  7. // 查看值和类型
  8. var_dump($username);
  9. $username="David";
  10. echo 'Hi '.$username . '<br/>';
  11. // 值传递
  12. $realname = $username;
  13. echo " Hi {$realname} <br/>";
  14. // 引用传递
  15. $getname = &$username;
  16. $getname = "Sarah";
  17. echo "Hello {$getname}: {$username} <br/>";
  18. // 函数
  19. function getUsername(string $username): string{
  20. return 'Hello,' . $username;
  21. }
  22. echo getUsername('Dave') . '<br/>';
  23. // 参数不足:默认值
  24. function sayHi(string $username="Dave"): string{
  25. return 'Hi,'. $username;
  26. }
  27. echo sayHi() . '<br/>';
  28. echo sayHi('php.cn') . '<br/>';
  29. // 声明一个匿名函数/函数表达式
  30. $sum = function (int $n1=0, int $n2=0): int{
  31. return $n1+$n2;
  32. };
  33. echo "{$sum(1,1)} <br/>";
  34. // 参数过多
  35. function add(...$arr): int{
  36. return array_reduce($arr,function($acc,$index){
  37. return $acc+$index;
  38. },0);
  39. };
  40. echo add(1,2,3,4,5,6,7,8,9,10) . '<br/>';
  41. // 返回值(闭包问题)
  42. function getLager(array $arr, $value): array{
  43. return array_filter($arr, function($item) use ($value){
  44. return $item > $value;
  45. });
  46. };
  47. $list = [1,2,3,4,5,6,7,8,9,10];
  48. print_r( getLager($list,5) );
Correcting teacher:PHPzPHPz

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