This article mainly introduces the PHP variable length parameter processing function, combined with the instance form Analysis Func_num_args (), Func_get_arg () and Func_get_args () three variable long parameter processing function function and the use method, the need friend can refer to the next
The example of PHP variable length parameter processing function is described in this paper. Share to everyone for your reference, as follows:
Like C + +, a function that contains mutable parameters can be processed in PHP, just as it is, and requires a function to handle a list of parameters specifically. Three related functions are available in PHP:
1. Func_num_args ()--Returns the number of arguments passed to the function
int func_num_args ( void )
Example:
function Open_database ($DB, $cache _size_or_values=null, $cache _size=null) { switch (Function_num_args ()) { Case 1: $r = select_db ($DB); break; Case 2: $r = select_db ($DB, $cache _size_or_values); break; Case 3: $r = select_db ($DB, $cache _size_or_values, $cache _size); break; } Return Is_resource ($R);}
2. Func_get_arg ()--Returns the specified parameter
mixed func_get_arg ( int arg_num)
The arg_num of the first parameter in the argument list is 0
<?php function Some_func ($a, $b) {for ($i = 0; $i <func_num_args (); + + $i) { $param = Func_get _arg ($i); echo "The param is $param \ n" } } Some_func (1,3,5,7,9);? >
3. Func_get_args ()--Returns the parameter list as an array
array func_get_args ( void )
Example:
<?php function Some_other_func ($a, $b) { $param = Func_get_args (); $param = Join (",", $param); Decomposition shows echo "The arglist is: $param \ n"; } Some_other_func (1,3,5,7,9);? >