Blogger Information
Blog 18
fans 0
comment 0
visits 7897
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
变量与函数的声明,使用与参数实例
只如初见
Original
376 people have browsed it

变量与函数的声明,使用与参数实例

代码

  1. <?php
  2. //变量与函数
  3. //变量声明
  4. $username = "小刘同学";
  5. //打印
  6. echo $username;
  7. //函数声明
  8. function getUsername(string $username):string
  9. {
  10. return '你好, '. $username;
  11. }
  12. //调用
  13. echo getUsername('李同学');
  14. //参数不足 ,默认值
  15. function getTotal(float $price,int $num = 1):float
  16. {
  17. return $price * $num;
  18. }
  19. echo '总金额:'.getTotal(55.5,5).' 元';
  20. //参数过多 剩余参数 ...rest
  21. $sum = function(float $a,float $b,float $c):float
  22. {
  23. return $a + $b +$c;
  24. };
  25. $sum = function(...$args){
  26. return array_reduce($args,function($acc,$cur){
  27. return $acc + $cur;
  28. });
  29. };
  30. echo $sum(3,4,5,6,7);
  31. //返回值
  32. // 默认返回单值:return
  33. // 返回多值:数组/对象
  34. $arr = [33,3,55,5,15,59,100];
  35. // 在php回调方法中,使用外部变量,用use进行声明
  36. function getItems(array $arr,$value):array
  37. {
  38. return array_filter($arr,function($item) use ($value){
  39. return $item>$value;
  40. });
  41. }
  42. print_r(getItems($arr,22));
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