Blogger Information
Blog 22
fans 0
comment 0
visits 11439
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
变量与函数
没耐心的渔翁
Original
643 people have browsed it

变量与函数的

变量声明用$

  1. //变量声明
  2. $user='PHP';
  3. //变量打印
  4. echo $user;
  5. //更新
  6. $user='朱老师';
  7. echo $user;
  1. //函数声明//和js声明一样 可以限定返回值
  2. function getTotal(float $price, int $num=2) :float{
  3. return $price * $num ;
  4. }
  5. $name='PHP';
  6. function getUser($name){
  7. return 'hollow,'. $name;
  8. }
  9. echo getUser('php').'<br>';
  10. echo getUser($name);

参数不足 给默认值

  1. function getTotal(float $price, int $num=2) :float{
  2. return $price * $num ;
  3. }
  4. echo '总金额:'.getTotal(55.5).'元<br>';

参数过多 用….rest

  1. $num = function(...$args){
  2. print_r($args);
  3. };
  4. $num(5,10,15,35);

参数相加

  1. $num= function(...$argc){
  2. return array_reduce($argc,function($acc,$cur){
  3. return $acc + $cur;
  4. });
  5. };
  6. echo $num(1,2,3,4,5,6,7,8,9);

返回只

  1. //返回单值用 return
  2. //返回多值使用数组或者对象
  3. $arr=[1,2,3,4,5,9];
  4. function getItme(array $arr,$value):array{
  5. return array_filter($arr,function($key) use($value){
  6. return $key>$value;
  7. });
  8. };
  9. print_r(getItme($arr,4));
  10. echo '<br>';
  11. echo implode(',',getItme($arr,4));
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