What to do if the php object parameters are not sure

藏色散人
Release: 2023-03-14 20:00:01
Original
1670 people have browsed it

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){ ...}}".

What to do if the php object parameters are not sure

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 &#39;<BR><BR>&#39;.func_get_arg($key);   //获取单个参数的值
        echo &#39;<BR>&#39;.$value;        //单个参数的值
    }
    var_export($args);  
}   
$parm_fir = &#39;name&#39;;
$parm_sec = &#39;sex&#39;;
uncertainParam($parm_fir, $parm_sec);
Copy after login

Optional parameters:

<?php
function mosaic($var1, $var2, $var3=&#39;c&#39;, $var4=&#39;d&#39;){
     return $var1+$var2+$var3+$var4;
}
$parm_fir = &#39;a&#39;;
$parm_sec = &#39;b&#39;;
$parm_three = &#39;c&#39;;
$parm_four = &#39;d&#39;;
echo mosaic($parm_fir , $parm_sec);    //输出&#39;ab&#39;
echo mosaic($parm_fir, $parm_sec, $parm_three); //输出&#39;abc&#39;
echo mosaic($parm_fir, $parm_sec, $parm_three, $parm_four);//输出&#39;abcd&#39;
echo mosaic($parm_fir);      //出错:必须给出第二个必填参数
echo mosaic($parm_fir, $parm_sec, , $parm_three);//出错:不能跳过任何一个可选参数而给出列表中后面的可选参数
?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!