Note 011PHP variable parameter function implementation

Source: Internet
Author: User
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 )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.