Home > Article > Backend Development > PHP functions func_get_args(), func_get_arg() and func_num_args()_PHP tutorial
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{
private $pass; private $host;
public function __construct(){ this->connect(); } } |
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);<🎜>
?>
|