Detailed explanation of func_get_args and func_num_args

高洛峰
Release: 2023-03-05 15:40:01
Original
1449 people have browsed it

func_get_args()—Returns an array. Each item in this array is a parameter of the function. According to the PHP manual, we give the usage format of the function.

array func_get_args ( void )
Copy after login
Copy after login

If we just give a general explanation here, you may not be able to truly understand this function, so let us look at the usage of this function through examples.

function foo() {     
 $args = func_get_args();   
 foreach ($args as $k => $v) {
  echo “arg”.($k+1).”: $v\n”;
  }  
}  
foo();  /* 没用任何输出*/  
foo(‘hello’);  /* 输出  arg1: hello  */ 
foo(‘hello’, ‘world’, ‘again’);  /*输出 arg1: hello  arg2: world  arg3: again  */
Copy after login
Copy after login

This function can put all the parameters you pass in into an array and then output it. Will this make it much easier for us to write PHP programs in the future?
Now that we are talking about the func_get_args function, we have to mention the func_num_args function and func_get_arg function

func_nums_args - counts the number of passed in function parameters

func_get_arg - according to the index Obtain a certain parameter, and the index number here is passed into the parameter of the function

Let’s take an example from the PHP manual

<?
 
function foo()
{
    $numargs = func_num_args();
    echo “Number of arguments: $numargs\n“;
}
 
foo(1, 2, 3);    // Prints ‘Number of arguments: 3′
?>
Copy after login
Copy after login

The above example clearly shows us the func_num_args function It is to pass the parameters of the function live

    <?php
    function foo()
    {
         $numargs = func_num_args();
         echo "Number of arguments: $numargs<br />\n";
         if ($numargs >= 2) {
         echo "Second argument is: " . func_get_arg(1) . "<br />\n";
         }
    }
     
    foo (1, 2, 3);
    //Prints
    //Number of arguments: 3
    //Second argument is: 2
    ?>
Copy after login
Copy after login

In the above example, func_get_arg(1) is to get the second parameter of the function. Okay, let's take a look at the comprehensive examples of these three functions, so that we can master these three functions.

<?php
function foo()
{
    $numargs = func_num_args();//得到参数的个数
    echo "Number of arguments: $numargs<br />\n";
    if ($numargs >= 2) {
        echo "Second argument is: " . func_get_arg(1) . "<br />\n";
    }
    $arg_list = func_get_args();
    for ($i = 0; $i < $numargs; $i++) {
        echo "Argument $i is: " . $arg_list[$i] . "<br />\n";
    }
}

foo(1, 2, 3);
/*Number of arguments: 3
Second argument is: 2
Argument 0 is: 1
Argument 1 is: 2
Argument 2 is: 3*/
?>
Copy after login
Copy after login

func_get_args()—Returns an array. Each item in this array is a parameter of the function. According to the PHP manual, we give the usage format of the function.

array func_get_args ( void )
Copy after login
Copy after login

If we just give a general explanation here, you may not be able to truly understand this function, so let us look at the usage of this function through examples.

function foo() {     
 $args = func_get_args();   
 foreach ($args as $k => $v) {
  echo “arg”.($k+1).”: $v\n”;
  }  
}  
foo();  /* 没用任何输出*/  
foo(‘hello’);  /* 输出  arg1: hello  */ 
foo(‘hello’, ‘world’, ‘again’);  /*输出 arg1: hello  arg2: world  arg3: again  */
Copy after login
Copy after login

This function can put all the parameters you pass in into an array and then output it. Will this make it much easier for us to write PHP programs in the future?
Now that we are talking about the func_get_args function, we have to mention the func_num_args function and func_get_arg function

func_nums_args - counts the number of passed in function parameters

func_get_arg - according to the index Obtain a certain parameter, and the index number here is passed into the parameter of the function

Let’s take the example in the PHP manual

<?
 
function foo()
{
    $numargs = func_num_args();
    echo “Number of arguments: $numargs\n“;
}
 
foo(1, 2, 3);    // Prints ‘Number of arguments: 3′
?>
Copy after login
Copy after login

The above example clearly shows us the func_num_args function It is to pass the parameters of the function live

    <?php
    function foo()
    {
         $numargs = func_num_args();
         echo "Number of arguments: $numargs<br />\n";
         if ($numargs >= 2) {
         echo "Second argument is: " . func_get_arg(1) . "<br />\n";
         }
    }
     
    foo (1, 2, 3);
    //Prints
    //Number of arguments: 3
    //Second argument is: 2
    ?>
Copy after login
Copy after login

In the above example, func_get_arg(1) is to get the second parameter of the function. Okay, let's take a look at the comprehensive examples of these three functions, so that we can master these three functions.

<?php
function foo()
{
    $numargs = func_num_args();//得到参数的个数
    echo "Number of arguments: $numargs<br />\n";
    if ($numargs >= 2) {
        echo "Second argument is: " . func_get_arg(1) . "<br />\n";
    }
    $arg_list = func_get_args();
    for ($i = 0; $i < $numargs; $i++) {
        echo "Argument $i is: " . $arg_list[$i] . "<br />\n";
    }
}

foo(1, 2, 3);
/*Number of arguments: 3
Second argument is: 2
Argument 0 is: 1
Argument 1 is: 2
Argument 2 is: 3*/
?>
Copy after login
Copy after login

For more detailed articles on func_get_args and func_num_args, please pay attention to the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!