This time to bring you the PHP dynamic access to function parameters of the steps in detail, PHP dynamic access to the function parameters of the note, the following is the actual case, together to see.
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
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
PHP implementation of the red envelope amount split algorithm case detailed
PHP dependency Inversion Case detailed