Variable number parameter function of PHP function

Source: Internet
Author: User
Tags php programming
PHP Variable number parameter function introduction

The default parameter is appropriate for cases where the number of arguments is less than the formal parameter, and the variable argument list is suitable for cases where the number of arguments is more than the formal parameter. It doesn't make sense if you don't use more arguments in the function.

In the previous section we introduced the PHP default parameter function, which describes the functions of PHP variable number parameters.

The function format of the variadic parameter is described as follows:

Mixed Funname (String arg1 [, String ...])    In the parameter list, use  ...  The parameters described

Typically, when a user defines a function, the number of parameters set is limited. If you want the function to accept any number of arguments, you need to use the Func_get_args () function provided by the PHP system in the function, which returns all arguments passed to the script function as an array.

Declares a function that prints the value of a parameter list. Although no argument list is declared, you can pass in any number of parameter values of any type. Return all arguments passed to the script function as an array, and then use the For loop to iterate through the array, output each parameter passed in to the function, and finally call the function and enter multiple parameters.

In fact the example code is as follows:

<?php function More_args () {   $args = Func_get_args ();      Returns all arguments passed to the script as an array for the for   ($i = 0; $i < count ($args), $i + +) {          //Use the FOR Loop     echo "No.". $i. " Parameters: ". $args [$i]." <br> ";    Output each parameter of the incoming function separately   }} More_args (1, 2, 3);       Call the function and enter multiple parameters?>

The output results are as follows:

No. 0 Parameter: 1

1th parameter: 2

2nd parameter: 3

In addition, you can use the Func_num_args () function to return the total number of parameters, use the Func_get_args () function to accept a numeric parameter, and return the specified argument. Here we need to delete all arguments passed to the script as an array return $args in the previous instance, and then use the Func_num_args () function to calculate the total number of parameters, using Func_get_arg to loop through each parameter of the incoming function. Finally, call this function and enter multiple parameters.

Its modified instance code is as follows:

<?php function More_args () {for   ($i = 0; $i <func_num_args (); $i + +) {          //use for Loop     echo "section". $i. " Parameters: ". Func_get_arg ($i)." <br> ";    Output each parameter of the incoming function separately   }} More_args (1, 2, 3);       Call the function and enter multiple parameters?>

The output results are as follows:

No. 0 Parameter: 1

1th parameter: 2

2nd parameter: 3

The two examples above implement the same functionality, and can be used in functions to get any number of argument lists in the function.

From the above example, it can be seen that the PHP function variable number of parameters like the following several forms:

Func_num_args The total number of parameters returned

func_get_arg Returns an item of a parameter list

Func_get_args Returns an array containing a list of function arguments

"Related tutorials Recommended"

1. "Php.cn lonely Nine Cheap (4)-php video Tutorial"

2. PHP programming from getting started to mastering the full set of video tutorials

3. PHP real-Combat video tutorial

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.