I often use variable parameter functions in Js, and I think it is very convenient to use them. I suddenly thought that PHP could also implement related functions? I often use variable parameter functions in Js, and I think it is very convenient to use them. I suddenly thought that PHP could also implement related functions?
When I asked Du Niang, du Niang told me that this idea is feasible. That is, during the search process, the func_get_arg and func_get_args functions entered my eyes. These two functions have been used before, and we generally know that there is such a thing, but at that time, because there was no such requirement, there was no such consideration, therefore, I did not have a deep understanding of the usage of the two functions.
Func_get_arg is used to obtain the specified parameter value in a function. Syntax format: func_get_arg (arg_num), where arg_num refers to the digital index of the parameter. In the world of programs, the first number is not 1, but 0. I don't have to say that.
Func_get_args is used to obtain all the parameter values in a function and return them in an array. This function does not need to pass parameters. This function is the key to implementing variable parameters.
You can use the func_get_args function in a function to obtain all the parameters of the function, and then perform logical processing on these parameters, that is, perform different logical operations based on different parameters, the variable parameter function we need is implemented.
There is also a function: func_num_args, which is used to return the number of parameters in the function. using this function, combined with the previously mentioned function, we can implement the functions we need more conveniently.
The specific example of Hy369 is not provided in the blog (in fact, this is an excuse for your laziness), understand the principle, and implement variable parameter functions should be no problem, after all, we are all legendary siege Lions, no, haha.
The above is the implementation of PHP variable parameters function in note 011. For more information, see PHP Chinese network (www.php1.cn )!