Home  >  Article  >  Backend Development  >  Detailed explanation of PHP variable length parameter processing function examples

Detailed explanation of PHP variable length parameter processing function examples

怪我咯
怪我咯Original
2017-06-28 09:22:181220browse

This article mainly introduces the PHP variable-length parameter processing function, and analyzes the functions and uses of the three variable-length parameter processing functions func_num_args(), func_get_arg() and func_get_args() combined with examples. For the method, friends who need it can refer to

The example in this article describes the PHP variable length parameter processing function. Share it with everyone for your reference, the details are as follows:

Like C++, functions containing variable parameters can also be processed in PHP. The principle is the same, and a function is needed to specifically process the parameters. list. PHP provides three related functions:

1. func_num_args() -- Returns the number of parameters passed to the function

int func_num_args (void)

Example:

function open_database($DB, $cache_size_or_values=null, $cache_size=null)
{
  switch (function_num_args())
  {
    case 1:
      $r = select_db($DB);
      break;
    case 2:
      $r = select_db($DB, $cache_size_or_values);
      break;
    case 3:
      $r = select_db($DB, $cache_size_or_values, $cache_size);
      break;
  }
  return is_resource($r);
}

2. func_get_arg() -- Return the specified parameters

mixed func_get_arg (int arg_num)

The arg_num of the first parameter in the parameter list is 0

<?php
  function some_func($a, $b)
  {
    for($i = 0; $i<func_num_args(); ++$i)
    {
      $param = func_get_arg($i);
      echo "the param is $param\n"
    }
  }
  some_func(1,3,5,7,9);
?>

3. func_get_args() -- Return as an array Parameter list

array func_get_args (void)

Example:

<?php
  function some_other_func($a, $b)
  {
    $param = func_get_args();
    $param = join(", ", $param);  // 分解显示
    echo "the arglist is: $param\n";
  }
  some_other_func(1,3,5,7,9);
?>


The above is the detailed content of Detailed explanation of PHP variable length parameter processing function examples. 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