The parameter list length of the php function can also be unfixed.

Source: Internet
Author: User
Today, we will introduce a small function that is not commonly used in php, but sometimes very useful: a function with a fixed number of parameters

Today, we will introduce a small function that is not commonly used in php, but sometimes very useful: a function with a variable number of parameters.

Many other languages (such as java-) should have this function: define a function func (){...}, When using a function, func ($ ){...}, It can also be func ($ a, $ B ){...} And so on. The parameter list of a function can be of an indefinite length. In fact, PHP can also achieve this "effect ".

Use a powerful function: func_get_args ():

// Defines a function my_demo without parameters.
Function my_demo (){
$ Args_arr = func_get_args (); // use the func_get_args function to save the currently passed parameters to the $ args_arr array.
Print_r ($ args_arr );
// $ Args_arr saves the input parameters, so you can work!
}
// Use
My_demo ('AAA', 'BBB ', 'CCC ');
My_demo (100,200 );
?>

In the PHP Manual, func_get_args (1) can use the second input parameter. func_get_args (2) can use the third input parameter...
Using func_get_args () to write functions should be more flexible and it is convenient to pass parameters.

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.