Home > Article > Backend Development > Explanation of variable parameter functions and optional parameter functions in php
1) Optional parameters Function . For example:
<?php function add($var1,$var2,$var3=0,$var4=0) { return $var1+$var2+$var3+$var4; } echo add(1,1); //输出2 echo add(1,1,1); //输出3 echo add(1,1,1,1);//输出4 echo add(1); //出错:必须给出参数2 echo add(1,1,,1);//出错:不能漏掉一个可选参数而给出列表中最后一个可选参数 ?>
Because $var3 and $var4 are given default values in defining the function , if not Pass values to them, that is, use default values, all are optional.
2)Variable parametersFunction
<?php function variable() { echo func_num_args(); //输出参数个数 $varArray = func_get_args; //获取参数,返回参数数组 foreach($varArray as $value) echo $value; echo func_get_arg; //获取单个参数 } ?>
The above is the detailed content of Explanation of variable parameter functions and optional parameter functions in php. For more information, please follow other related articles on the PHP Chinese website!