PHP Dynamic Get function parameters

Source: Internet
Author: User

PHP supports a variable number of parameter lists in user-defined functions. It's really simple, just use Func_num_args () , func_get_arg () , and the Func_get_args () function. mutable parameters do not require special syntax, and parameter lists are still passed to the function as defined by the function and are used in the usual way.   1.func_num_args -Returns the total number of arguments passed in the function
int Func_num_args (void)
Example
<?phpfunction  Demo () {    $numargs  =  Func_num_args ();    echo  "Number of parameters:  $numargs \ n";} Demo (' A ',  ' B ',  ' C ');
Run Results
Number of parameters: 3
2.func_get_args-Returns the argument list of the passed-in function
Array Func_get_args  (void)
Example
<?phpfunction  Demo () {    $args = Func_get_args ();    echo "Incoming parameters are:";    Var_dump ($args);} Demo (' A ',  ' B ',  ' C ');

Run results

The parameters passed are: Array (size=3)  0 = = String ' A ' (length=1)  1 = string ' B ' (length=1)  2 = = String ' C ' (len gth=1)
3.func_get_arg-returning parameter values from the parameter list based on the parameter index
Mixed  func_get_arg  (int $arg _num  )
Example
<?phpfunction  Demo () {    $numargs  =  Func_num_args ();    echo  "Number of parameters:  $numargs <br/>";    $args = Func_get_args ();    if ($numargs  >=  2) {        echo  "The second parameter is:"  .  Func_get_arg (1).  " <br/> ";    }} Demo (' A ',  ' B ',  ' C ');
Run Results
The number of parameters is: 3 The second parameter is: b

PHP Dynamic Get function parameters

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.