This article mainly introduces the method of using a variable number of parameters in PHP functions. It has a certain reference value. Now I share it with you. Friends in need can refer to it.
php in user-defined functions Supports a variable number of parameter lists.
In php5.5 and earlier versions, use thefunc_num_args(), func_get_arg(), func_get_args()function implementation.
Output:
1 a Array ( [0] => a ) 3 1 Array ( [0] => 1 [1] => 2 [2] => 3 ) 2 Array ( [0] => d [1] => e ) Array ( [0] => Array ( [0] => d [1] => e ) [1] => Array ( [0] => f ) )
in php5.6 and above version, you can use... syntax.
Example 1: Use...$args to replace any number of parameters
The output result is the same as php5. 5 Use func_num_args(), func_get_arg(), func_get_args() functions consistently.
Example 2: Convert array to parameter list
Example 3: Some parameters are specified, others The number of parameters is variable
Output:
name:fdipzone tag:programmer args: Array ( ) name:terry tag:designer args: Array ( [0] => 1 [1] => 2 ) name:aoao tag:tester args: Array ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [0] => c ) [2] => Array ( [0] => d ) )
Related recommendations:
php function mkdir implements the method of recursively creating hierarchical directories
The above is the detailed content of PHP function uses variable number of parameters method. For more information, please follow other related articles on the PHP Chinese website!