Home  >  Article  >  Backend Development  >  PHP function variable number of parameter lists

PHP function variable number of parameter lists

不言
不言Original
2018-03-30 15:24:161462browse

This article introduces you to the variable number of parameter lists of PHP functions. Friends in need can refer to

PHP5.5 and earlier versions

Use the following Function:

  • func_num_args Returns the total number of parameters

  • ##func_get_arg Returns a certain number in the parameter list One item

  • func_get_args Returns an array containing the function argument list

  • function test()
    {
        echo '参数总数;', func_num_args(), "\n";
        echo '第一个参数:', func_get_arg(0), "\n";
        echo '全部参数;';
        print_r(func_get_args());
    }
    
    test(1, 2, 3, 4);
    
    /*
    参数总数;4
    第一个参数:1
    全部参数;Array
    (
       [0] => 1
       [1] => 2
       [2] => 3
       [3] => 4
    )
    */
PHP5.6 and above

Use

... syntax implementation

function test(...$params)
{
    print_r($params);
}

test(1, 2, 3, 4);

/*
Array         
(             
    [0] => 1  
    [1] => 2  
    [2] => 3  
    [3] => 4  
)
*/

Related recommendations:                                                                                                                                  

Notes on using PHP functions Notes on using solid state drives Notes on using ID cards etc Notes on using

Notes on PHP functions

The above is the detailed content of PHP function variable number of parameter lists. 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