This article will summarize the usage and differences between the func_get_args (), func_get_arg (), and func_num_args () functions in php.
These built-in functions are used today to record them for future query.
Func_num_args () returns the number of input parameters for calling the function. The type is integer.
Func_get_arg () returns the specified parameter value.
Func_get_args () returns the values of all parameters. The type is array.
Func_get_args
Returns an array containing the list of function parameters.
Description
Array func_get_args (void)
This function returns an array. Each element of the array is equivalent to the parameters of the function defined by the user currently. If you call this function outside the function definition, func_get_args () generates a warning.
Func_get_arg
Return the specified parameter from the function parameter list.
Description
Mixed func_get_arg (int arg_num)
Returns the arg_num parameter of the parameter list of the defined function. The parameter starts from 0. When you call this function outside the function definition, a warning is generated. When arg_num is greater than the number of parameters actually passed by the function, a warning is generated and FALSE is returned.
--------------------------------------------------------------------------------
Func_num_args
Returns the number of parameters passed to the function.
Description
Int func_num_args (void)
This function returns the number of parameters passed to the current defined function. If you call this function outside the function definition, func_num_args () generates a warning.
Func_num_args () can be used in combination with func_get_arg () and func_get_args () to allow user-defined functions to accept the variable length parameter list. When we build PHP classes, flexible use of these three functions can play an ideal effect. For example, when creating a class with PHP and MYSQL links, we can write the following code:
| The Code is as follows: |
Copy code |
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 ();
}
}
........
?> |
The following is an example in 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