您的位置 首页 技术专栏

php对象参数不确定怎么办

24小时课堂在线收录php对象参数不确定怎么办,php对象参数不确定的时候需要改变一下写法,修改后的代码如“function uncertainParam() {$args = func_…感谢您关注php对象参数不确定怎么办。

php对象参数不确定的时候需要改变一下写法,修改后的代码如“function uncertainParam() {$args = func_get_args();foreach($args as $key=>$value){…}}”。

本文操作环境:windows7系统、PHP7.1版、DELL G3电脑

php对象参数不确定怎么办?

php不定参数方法(函数)和可选参数方法(函数)

写代码的时候经常会用到方法,而且往往还是带有参数的方法,这些对我们来说都不陌生,然而有时候需要使用的方法参数的个数不确定,这样我们就需要改变一下写法了,如下:

<?phpfunction 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);

可选参数:

<?phpfunction 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);//出错:不能跳过任何一个可选参数而给出列表中后面的可选参数?>

以上就是php对象参数不确定怎么办的详细内容,更多请关注24小时课堂在线其它相关文章!

本文来自网络,不代表24小时课堂在线立场,转载请注明出处:https://www.24ketang.cn/103262.html

为您推荐

返回顶部