Variable number parameter functions of php functions

Source: Internet
Author: User
The default parameter is applicable when the number of real parameters is less than the actual parameter, while the variable parameter list is applicable when the number of real parameters is more than the actual parameter. If multiple input parameters are not used in the function, it is meaningless. Php variable number parameter functions

The default parameter is applicable when the number of real parameters is less than the actual parameter, while the variable parameter list is applicable when the number of real parameters is more than the actual parameter. If multiple input parameters are not used in the function, it is meaningless.

The previous section describes default php parameter functions. This section describes the functions of php variable number parameters.

Variable parameter functionsThe format is described as follows:

Mixed funName (string arg1 [, string...]) // The parameters described with... are displayed in the parameter list.

Generally, when you define a function, the number of parameters you set is limited. If you want the function to accept any number of parameters, you need to useFunc_get_args ()Function, which returns all parameters passed to the script function as an array.

Declare a function to print the value of the parameter list. The parameter list is not declared, but any number or type of parameter values can be input. First, return all parameters passed to the script function as an array, then use the for loop to traverse the array, output each parameter of the input function, and finally call the function and input multiple parameters.

The instance code is as follows:

 "; // Output each parameter of the input function} more_args (1, 2, 3); // call the function and input multiple parameters?>

The output result is as follows:

0th parameters: 1

1st parameters: 2

2nd parameters: 3

You can also use the func_num_args () function to return the total number of parameters. use the func_get_args () function to receive a numeric parameter and return the specified parameter. Here we need to delete all the parameters passed to the script in the previous instance as $ args returned by an array, and then use the func_num_args () function to calculate the total number of parameters, use func_get_arg to cyclically output each parameter of the input function. Finally, call this function and enter multiple parameters.

The modified instance code is as follows:

 "; // Output each parameter of the input function} more_args (1, 2, 3); // call the function and input multiple parameters?>

The output result is as follows:

0th parameters: 1

1st parameters: 2

2nd parameters: 3

The two examples above implement the same function. you can obtain any number of parameter lists in the function and use them in the function.

From the above example, we can see that the variable number of parameters of the PHP function has the following forms:

Func_num_argsTotal number of returned parameters

Func_get_argReturns an item in the parameter list.

Func_get_argsReturns an array containing the list of function parameters.

[Recommended tutorials]

1. php1.cn Dugu jiujian (4)-php video tutorial

2. php programming from getting started to mastering a full set of video tutorials

3. php video tutorial

The above is a detailed description of the variable number parameter function of php functions. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.