Home > Backend Development > PHP Problem > How to get how many parameters a function has in php

How to get how many parameters a function has in php

藏色散人
Release: 2023-03-13 10:58:01
Original
2562 people have browsed it

php method to get several parameters of a function: 1. Return the total number of parameters passed to the function through the func_num_args function; 2. Return the parameter list of the passed function through the func_get_args function; 3. Return the parameter list from the function passed through the func_get_arg function. Parameter list returns the parameter value.

How to get how many parameters a function has in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How does php get the parameters of the function? ?

PHP method to dynamically obtain function parameters:

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

int func_num_args ( void )
Copy after login

Example

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

Running result

The number of parameters is: 3

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

array func_get_args  ( void )
Copy after login

Example

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

Run result

The parameters passed in are:

array (size=3)
  0 => string &#39;a&#39; (length=1)
  1 => string &#39;b&#39; (length=1)
  2 => string &#39;c&#39; (length=1)
Copy after login

3. func_get_arg — Return parameter value from parameter list according to parameter index

mixed  func_get_arg  ( int $arg_num  )
Copy after login

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; );
Copy after login

Run result

The number of parameters is: 3

The second parameter is: b

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to get how many parameters a function has in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template