Php variable length parameter processing function details, php Variable Function details
This example describes the php variable length parameter processing function. We will share this with you for your reference. The details are as follows:
Like C ++, PHP can also process functions that contain variable parameters. The truth is the same. A function is required to process the parameter list. PHP provides three related functions:
1. func_num_args () -- returns the number of parameters 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 () -- return the specified parameter
mixed func_get_arg ( int arg_num)
The arg_num of the first parameter in the parameter 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 ); // display echo "the arglist is: $ param \ n";} some_other_func (1, 3, 5, 7, 9);?>