This article mainly introduces php variable parameter implementation. if you are interested in the PHP Tutorial, you can refer to it. Editor's note: The first thought was to use arrays or macros in the C language. However, if php implementation methods are different on the Internet, I will repost the article.
First, we need to write a PHP function to calculate the sum of two numbers, which can be written as follows:
/*** Calculate the sum of two numbers and return the calculation result * @ param number $ a * @ param number $ B * @ return number */function sum ($, $ B) {return $ a + $ B ;}
Similarly, if we need to calculate the sum of three numbers, we can write it as follows:
/*** Calculate the sum of two or three numbers, and return the calculated result * @ param number $ a * @ param number $ B * @ return number $ c. This parameter can be left blank, the default value is 0 */function sum ($ a, $ B, $ c = 0) {return $ a + $ B + $ c ;}
Now, if we need to calculate any number of sums, how do we compile PHP functions?
Of course, you may consider using arrays as the parameter passed by the function to implement this function:
/*** Calculate any number of sums. the function parameter params must be of the array type * @ param array params */function sum ($ params) {$ total = 0; foreach ($ params as $ I) {$ total + = $ I;} return $ total ;}
Well, this is indeed correct, because in the process of program development before the birth of variable parameters, when you need to pass any number of parameters, are represented by arrays or other similar sets. However, isn't the transfer clear and intuitive? As a PHP programmer, you should know that there is a function var_dump () in PHP that is used to display variable details. for example:
$ Age = 18; var_dump ($ age); // display the details of the variable $ age
When you need to display the information of multiple variables, you can also use the following:
$ Name = 'Zhang San'; $ age = 18; $ gender = true; var_dump ($ name, $ age, $ gender );
We know that var_dump () can receive any number of variables at the same time, and does not need to be passed in the form of an array. this parameter transmission method is more intuitive and elegant. This form of passing any number of parameters is called variable parameters. Of course, our sum () function can also be implemented in this way:
/*** Calculate any number of sums and return the calculated result */function sum () {// no parameter is defined in the brackets here $ total = 0; // use func_get_args () to obtain all the actual passing parameters of the current function. the returned value is array type $ varArray = func_get_args (); foreach ($ varArray as $ var) {$ total + = $ var;} return $ total ;}
/***** The following is a call example *****/
Echo sum (, 5); // calculate 1 + 3 + 5 echo sum (); // calculate 1 + 2 echo sum ); // calculate 1 + 2 + 3 + 4
As shown in the preceding example, you can use the PHP built-in function func_get_args () in the current function to call the actual parameter array passed by the function. then, you only need to process the parameter array.
Note 1: 1. If no parameter is input during the call, the function func_get_args () returns an array type, but an empty array (the array does not contain any element ). 2. func_get_args () can only be called in the function; otherwise, a warning message is displayed. 3. the func_get_args () function can receive an index parameter, which is used to obtain the parameter at the specified index in the parameter array. For example, to obtain the first passed parameter, you can call func_get_args (1 ). 4. you can also call func_num_args () in the function to return the number of parameters passed in by the current function call.
Note 2: the implementation of PHP variable parameters is very similar to that of JavaScript variable parameters. PHP uses the built-in function func_get_args () for implementation. JavaScript uses the built-in function variable arguments for implementation.
Note 3: In the final sum () function code, the sum () function does not define any form parameter. Therefore, when calling this function, you can input 0, 1, 2 ~ N parameters. However, in general, the calculation requires at least two numbers. Therefore, you can define two formal parameters in the definition of the sum () function, for example, sum ($ a, $ B). other code remains unchanged. In this way, at least two parameters must be input when the function is called.
Note 4: As PHP has built in the array_sum () function for calculating the sum of all elements in the array, the final version of the above code is as follows:
/*** Calculate any number of sums and return the calculated result */function sum ($ a, $ B) {return array_sum (func_get_args ());}
The above describes the php variable parameter implementation, including the content, hope to be helpful to friends who are interested in the PHP Tutorial.
Related articles:
How does php pass in each element of the array as the real parameter of the variable parameter function?
Php variable parameters
Differences between passing variable parameters to functions in JS and PHP instance code