Home  >  Article  >  Backend Development  >  Related content about php functions using a variable number of parameters

Related content about php functions using a variable number of parameters

jacklove
jackloveOriginal
2018-06-08 17:36:111315browse

php supports variable number of parameter lists in user-defined functions.

In php5.5 and earlier versions, use the func_num_args(), func_get_arg(), func_get_args() function implementation.

<?phpfunction myfunc(){

    // 获取参数数量
    echo func_num_args().PHP_EOL;    // 获取第一个参数的值:
    print_r(func_get_arg(0));    echo PHP_EOL;    // 获取所有参数的值
    print_r(func_get_args());    echo PHP_EOL;

}

myfunc(&#39;a&#39;);
myfunc(1, 2, 3);
myfunc(array(&#39;d&#39;,&#39;e&#39;), array(&#39;f&#39;));?>

Output:

1aArray(
    [0] => a
)31Array(
    [0] => 1
    [1] => 2
    [2] => 3)2Array(
    [0] => d
    [1] => e
)Array(
    [0] => Array
        (
            [0] => d
            [1] => e
        )

    [1] => Array
        (
            [0] => f
        )

)

In php5.6 and above, you can use Grammar implementation.

Example 1: Use...$args to replace any number of parameters

<?phpfunction myfunc(...$args){

    // 获取参数数量
    echo count($args).PHP_EOL;    // 获取第一个参数的值:
    print_r($args[0]);    echo PHP_EOL;    // 获取所有参数的值
    print_r($args);    echo PHP_EOL;

}

myfunc(&#39;a&#39;);
myfunc(1, 2, 3);
myfunc(array(&#39;d&#39;,&#39;e&#39;), array(&#39;f&#39;));?>

The output results are the same as php5.5 using func_num_args(), func_get_arg() , the func_get_args() function is consistent.


Example 2: Convert array to parameter list

<?phpfunction add($a, $b){
    echo $a + $b;
}$args = array(1, 2);

add(...$args); // 输出3?>


Example 3: Partial parameters specified, others The number of parameters is variable

<?phpfunction display($name, $tag, ...$args){
    echo &#39;name:&#39;.$name.PHP_EOL;    echo &#39;tag:&#39;.$tag.PHP_EOL;    echo &#39;args:&#39;.PHP_EOL;
    print_r($args);    echo PHP_EOL;
}

display(&#39;fdipzone&#39;, &#39;programmer&#39;);
display(&#39;terry&#39;, &#39;designer&#39;, 1, 2);
display(&#39;aoao&#39;, &#39;tester&#39;, array(&#39;a&#39;,&#39;b&#39;), array(&#39;c&#39;), array(&#39;d&#39;));?>

Output:

name:fdipzone
tag:programmer
args:Array(
)

name:terry
tag:designer
args:Array(
    [0] => 1
    [1] => 2)

name:aoao
tag:tester
args:Array(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => c
        )

    [2] => Array
        (
            [0] => d
        )

)

This article explains the issues related to PHP functions using a variable number of parameters. For more information, please visit Follow php Chinese website.

Related recommendations:

How to call Sina API through php to generate short links

Explain mysql group by sorting within the group Method

How to use php to reflect API to obtain class information

The above is the detailed content of Related content about php functions using a variable number of parameters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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