Home  >  Article  >  Backend Development  >  How to get all parameters in method in php

How to get all parameters in method in php

王林
王林Original
2021-02-20 15:28:374743browse

php method to obtain all parameters in a method: You can use the func_get_args function to achieve this, such as [$numargs = func_num_args();echo "number of parameters";].

How to get all parameters in method in php

The operating environment of this article: windows10 system, php 7.3, thinkpad t480 computer.

Function introduction:

func_get_args, get all parameters of a function

Specific code:

function foo()
{
    $numargs = func_num_args(); //参数数量
    echo "参数个数是: $numargs<br />\n";
    if ($numargs >= 2) {
        echo "第二个参数的值:" . func_get_arg(1) . "<br />\n";
    }
    $arg_list = func_get_args();
    for ($i = 0; $i < $numargs; $i++) {
        echo "第{$i}个参数值:{$arg_list[$i]}<br />\n";
    }
}
 
foo(1, &#39;d&#39;, 3,4);

Output:

参数个数是: 4
第二个参数的值:d
第0个参数值:1
第1个参数值:d
第2个参数值:3
第3个参数值:4

Related recommendations :php tutorial

The above is the detailed content of How to get all parameters in method in php. 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