$value){...}}"."/> $value){...}}".">
Home > Article > Backend Development > What to do if the php object parameters are not sure
When the php object parameters are uncertain, you need to change the writing method. The modified code is such as "function uncertainParam() {$args = func_get_args();foreach($args as $key=>$value){ ...}}".
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
What should I do if the php object parameters are not sure?
php indefinite parameter method (function) and optional parameter method (function)
Methods are often used when writing code, and they are often methods with parameters. These It is all familiar to us, but sometimes the number of method parameters that need to be used is uncertain, so we need to change the writing method, as follows:
<?php function uncertainParam() { $numargs = func_num_args(); //获得传入的所有参数的个数 echo "参数个数: $numargs\n"; $args = func_get_args(); //获得传入的所有参数的数组 foreach($args as $key=>$value){ echo '<BR><BR>'.func_get_arg($key); //获取单个参数的值 echo '<BR>'.$value; //单个参数的值 } var_export($args); } $parm_fir = 'name'; $parm_sec = 'sex'; uncertainParam($parm_fir, $parm_sec);
Optional parameters:
<?php function mosaic($var1, $var2, $var3='c', $var4='d'){ return $var1+$var2+$var3+$var4; } $parm_fir = 'a'; $parm_sec = 'b'; $parm_three = 'c'; $parm_four = 'd'; echo mosaic($parm_fir , $parm_sec); //输出'ab' echo mosaic($parm_fir, $parm_sec, $parm_three); //输出'abc' echo mosaic($parm_fir, $parm_sec, $parm_three, $parm_four);//输出'abcd' echo mosaic($parm_fir); //出错:必须给出第二个必填参数 echo mosaic($parm_fir, $parm_sec, , $parm_three);//出错:不能跳过任何一个可选参数而给出列表中后面的可选参数 ?>
Recommended learning: "PHP video tutorial"
The above is the detailed content of What to do if the php object parameters are not sure. For more information, please follow other related articles on the PHP Chinese website!