PHP supports a variable number of parameter lists in user-defined functions.
In php5.5 and earlier versions, the Func_num_args (), Func_get_arg (), and Func_get_args () functions were implemented.
<?phpfunction MyFunc () { //Get the number of parameters echo Func_num_args (). Php_eol; Gets the value of the first parameter: print_r (func_get_arg (0)); echo Php_eol; Gets the value of all parameters Print_r (Func_get_args ()); echo Php_eol;} MyFunc (' a '); MyFunc (1, 2, 3); MyFunc (Array (' d ', ' e '), array (' F '));? >
Output:
1aArray ( [0] = a) 31Array ( [0] = 1 [1] = 2 [2] + 3) 2Array ( [0] = + d [1] = > E) Array ( [0] = = Array ( [0] = + d [1] = + e ) [1] = = Array ( [0] + F ))
In versions php5.6 and above, you can use ... Syntax implementation.
Example 1: Using the ... $args instead of any number of parameters
<?phpfunction myfunc (... $args) { //Get the number of parameters echo count ($args). Php_eol; Gets the value of the first parameter: print_r ($args [0]); echo Php_eol; Gets the value of all parameters Print_r ($args); echo Php_eol;} MyFunc (' a '); MyFunc (1, 2, 3); MyFunc (Array (' d ', ' e '), array (' F '));? >
The output is consistent with php5.5 using Func_num_args (), Func_get_arg (), and the Func_get_args () function.
Example 2: Array to parameter list
<?phpfunction Add ($a, $b) { echo $a + $b;} $args = Array (1, 2); Add (... $args); Output 3?>
Example 3: Partial parameter designation, variable number of other parameters
<?phpfunction display ($name, $tag, ... $args) { echo ' name: '. $name. Php_eol; Echo ' tag: '. $tag. Php_eol; echo ' args: '. Php_eol; Print_r ($args); echo Php_eol;} Display (' Fdipzone ', ' Programmer ');d isplay (' Terry ', ' Designer ', 1, 2);d isplay (' Aoao ', ' tester ', Array (' A ', ' B '), Array ( ' C '), Array (' d '));? >
Output:
Name:fdipzonetag:programmerargs:Array () name:terrytag:designerargs:Array ( [0] = 1 [1] = 2) Name: Aoaotag:testerargs:Array ( [0] = = Array ( [0] = a [1] = b ) [1] = = Array ( [0] = c ) [2] = = Array ( [0] = d ))