Examples of how PHP functions use a variable number of parameters

Source: Internet
Author: User

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        ))

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.