A method for dynamically acquiring function parameters by PHP

Source: Internet
Author: User
This article mainly introduces the PHP implementation of dynamic access to function parameters of the method, now share to everyone, the need for friends can refer to the next

In this paper, we describe the method of implementing dynamic access function parameters in PHP. Share to everyone for your reference, as follows:

PHP supports a variable number of parameter lists in user-defined functions. In fact, it's simple, just use func_num_args() , func_get_arg() and 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 in are:
Array (size=3)
0 = String ' a ' (length=1)
1 = string ' B ' (length=1)
2 = String ' C ' (length=1)

3. func_get_arg-returns 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

Number of parameters: 3
The second parameter is: b


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.