Home  >  Article  >  Backend Development  >  How to dynamically obtain function parameter example code in php

How to dynamically obtain function parameter example code in php

伊谢尔伦
伊谢尔伦Original
2017-06-26 13:51:291278browse

PHP supports variable number of argument lists in user custom functions. It's actually very simple, just use func_num_args() , func_get_arg() , and func_get_args() function.

Variable parametersNo special syntax is required. The parameter list is still passed to the function in the same way as the function definition, and these parameters are used in the usual way.

1.func_num_args — Returns the total number of parameters passed into the function

int func_num_args ( void )

Example

<?php
function  demo ()
{
    $numargs  =  func_num_args ();
    echo  "参数个数为:  $numargs \n" ;
}
demo ( &#39;a&#39; ,  &#39;b&#39; ,  &#39;c&#39; );
?>

Running result

参数个数为: 3

2.func_get_args - Returns the parameter list passed into the function

array func_get_args  ( void )

Example

<?php
function  demo ()
{
    $args = func_get_args();
    echo "传入的参数分别为:";
    var_dump($args);
}
demo ( &#39;a&#39; ,  &#39;b&#39; ,  &#39;c&#39; );
?>

Running result

传入的参数分别为:
array (size=3)
  0 => string &#39;a&#39; (length=1)
  1 => string &#39;b&#39; (length=1)
  2 => string &#39;c&#39; (length=1)

3.func_get_arg — Return parameter value from parameter list based on parameter index

mixed  func_get_arg  ( int $arg_num  )

Example

<?php
function  demo ()
{
    $numargs  =  func_num_args ();
    echo  "参数个数为:  $numargs <br />" ;
    $args = func_get_args();
    if ( $numargs  >=  2 ) {
        echo  "第二个参数为: "  .  func_get_arg ( 1 ) .  "<br />" ;
    }
}
demo ( &#39;a&#39; ,  &#39;b&#39; ,  &#39;c&#39; );
?>

Run result

参数个数为: 3
第二个参数为: b

The above is the detailed content of How to dynamically obtain function parameter example code in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn