This article mainly introduces the PHP implementation of dynamic access to function parameters of the method, now share to everyone, the need for friends can refer to the next
In this paper, we describe the method of implementing dynamic access function parameters in PHP. Share to everyone for your reference, as follows:
PHP supports a variable number of parameter lists in user-defined functions. In fact, it's simple, just use func_num_args() , func_get_arg() and func_get_args() function.
mutable parameters do not require special syntax, and parameter lists are still passed to the function as defined by the function and are used in the usual way.
1. func_num_args-returns the total number of arguments passed in the function
int func_num_args ( void )
Example
<?phpfunction Demo () { $numargs = Func_num_args (); echo "Number of parameters: $numargs \ n";} Demo (' A ', ' B ', ' C ');
Run results
Number of parameters: 3
2. func_get_args-returns the argument list of the passed-in function
array func_get_args ( void )
Example
<?phpfunction Demo () { $args = Func_get_args (); echo "Incoming parameters are:"; Var_dump ($args);} Demo (' A ', ' B ', ' C ');
Run results
The parameters passed in are:
Array (size=3)
0 = String ' a ' (length=1)
1 = string ' B ' (length=1)
2 = String ' C ' (length=1)
3. func_get_arg-returns parameter values from the parameter list based on the parameter index
mixed func_get_arg ( int $arg_num )
Example
<?phpfunction Demo () { $numargs = Func_num_args (); echo "Number of parameters: $numargs <br/>"; $args = Func_get_args (); if ($numargs >= 2) { echo "the second parameter is:". Func_get_arg (1). "<br/>"; }} Demo (' A ', ' B ', ' C ');
Run results
Number of parameters: 3
The second parameter is: b