Home >Backend Development >PHP Tutorial >Analyze the use of func_num_args and func_get_args functions_PHP tutorial

Analyze the use of func_num_args and func_get_args functions_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:04:03823browse

func_num_args function function – returns the number of parameters passed to the function, its syntax is as follows : int func_num_args (void).
Description: Returns the number of parameters passed to the currently defined function. func_get_arg() will generate a warning if this function is called from outside the function definition.
func_num_args( ) can be used in conjunction with func_get_arg( ) and func_get_args( ) to allow user-defined functions to accept variable-length argument lists. Among them, func_get_arg() returns items from the parameter list, its syntax: int func_get_arg (int arg_num), returns the arg_numth parameter of the parameter list that defines the function, and its parameters start 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.
The difference between the func_get_args() function and the func_get_arg() function is that the func_get_args() function returns an array, and each element of the array is equivalent to the number of parameter columns of the current user-defined function.

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, can write the following code:

Copy code The code is as follows:

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();
}
}
…………omitted…………
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327800.htmlTechArticlefunc_num_args function function – returns the number of parameters passed to the function, its syntax is as follows: int func_num_args (void). Description: Returns the number of parameters passed to the currently defined function. If it is...
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