Home  >  Article  >  Backend Development  >  PHP functions func_get_args(), func_get_arg() and func_num_args()_PHP tutorial

PHP functions func_get_args(), func_get_arg() and func_num_args()_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 10:59:29974browse

This article will summarize and compare the usage and differences of the three functions func_get_args(), func_get_arg() and func_num_args() in PHP. Please refer to them for reference. ​

I used these built-in functions today, record them for review
func_num_args() Returns the number of parameters passed in to the calling function, the type is integer

func_get_arg() Returns the specified parameter value

func_get_args() Returns the value of all parameters, the type is array

func_get_args
Returns an array containing the function parameter list

Description

array func_get_args( void )
This function returns an array, and each element of the array is equivalent to each parameter of the current user-defined function. func_get_args() will generate a warning if this function is called from outside the function definition.


func_get_arg
Return the specified parameters from the function parameter list

Description


mixed func_get_arg( int arg_num )
Returns the arg_numth parameter of the parameter list that defines the function, with parameters starting from 0. And calling this function outside the function definition will generate a warning; and when arg_num is greater than the number of parameters actually passed by the function, a warning will also be generated and FALSE will be returned.


-------------------------------------------------- ----------------------------------


func_num_args
Returns the number of arguments passed to the function

Description


int func_num_args( void )
This function returns the number of arguments passed to the currently defined function. func_num_args() will generate a warning if this function is called from outside the function definition.

func_num_args() can be used in combination with func_get_arg() and func_get_args() to allow user-defined functions to accept variable-length parameter lists. When we build a PHP class, flexibly using these three functions can achieve very ideal results. For example, when creating a class that links PHP and MYSQL, you can write the following code:

The code is as follows Copy code
class mydb{
 代码如下 复制代码
class mydb{
private $user;
private $pass;
private $host;
private $db;
public function __construct(){
$num_args=func_num_args();
if($num_args>0){
$args=func_get_args();
$this->host=$args[0];
$this->user=$args[1];
$this->pass=$args[2];
this->connect();
}
}
……..
?>
private $user;

private $pass;

private $host;
 代码如下 复制代码

function foo()
{
    $numargs = func_num_args();
    echo "Number of arguments: $numargs
n";
    if ($numargs >= 2) {
        echo "Second argument is: " . func_get_arg(1) . "
n";
    }
    $arg_list = func_get_args();
    for ($i = 0; $i < $numargs; $i++) {
echo "Argument $i is: " . $arg_list[$i] . "
n";
}
}

foo(1, 2, 3);
?>

private $db;

public function __construct(){
$num_args=func_num_args();
if($num_args>0){
$args=func_get_args();
$this->host=$args[0];
$this->user=$args[1];

$this->pass=$args[2];
this->connect(); }

}

……..
Here is an example from the manual:
The code is as follows Copy code
function foo() { $numargs = func_num_args(); echo "Number of arguments: $numargs n"; If ($numargs >= 2) { echo "Second argument is: " . func_get_arg(1) . " n"; } $arg_list = func_get_args(); for ($i = 0; $i < $numargs; $i++) { echo "Argument $i is: " . $arg_list[$i] . "
n";
}<🎜> }<🎜> <🎜>foo(1, 2, 3);<🎜> ?>
The output result is: Number of arguments: 3 Second argument is: 2 Argument 0 is: 1 Argument 1 is: 2 Argument 2 is: 3 http://www.bkjia.com/PHPjc/445610.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445610.htmlTechArticleThis article will summarize the three functions of func_get_args(), func_get_arg() and func_num_args() in php Please refer to the usage and difference comparison of functions. I used this today...
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