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.