Php 简明教程

PHP - Variable Arguments

在 PHP 中,可以编写一个能够接受具有可变数量元素的自变量列表的函数。要声明可变自变量列表,应在自变量名称前加上“…​”(三个点)符号。所传递的值将收集到一个数组中,该数组名称是自变量的名称。

In PHP, it is possible to write a function capable of accepting a list of arguments with variable number of elements. To declare a variable argument list, the name of the argument is prepended by the "…​" (three dots) symbol. The values passed are collected into an array with the argument’s name.

function myfunction(...$arg) {
   Statement1;
   Statement2;
}

要调用这样的函数,请在圆括号中添加任意数量用逗号分隔的值。

To call such a function, put any number of comma-separated values in the parenthesis.

myfunction(v1, v2, v3, . . . , vn);

在函数中声明的形式自变量是传递的所有值的一个数组。我们可以使用任何适当的内置数组函数来执行此过程。

The formal argument declared in the function is an array of all the values passed. We van use any of the appropriate built_in array functions to perform the process.

Example

在以下示例中,用户定义的函数 myfunction() 能够接收可变数量的值并找到它们的平均值。

In the following example, the user defined function myfunction() is capable of receiving variable number of values and finds their average.

<?php
   function  myfunction(...$numbers) {
      $avg = array_sum($numbers)/count($numbers);
      return $avg;
   }
   $avg = myfunction(5, 12, 9, 23, 8);
   echo "average = $avg";
?>

它将生成以下 output

It will produce the following output

average = 11.4

尝试更改所传递数组的大小,并再次运行该程序。

Try changing the size of the passed array and run the program again.

可以在函数里面使用 foreach 循环来遍历数组。该函数可以在可变长度的自变量之前具有任何位置自变量。在所接收的值中,位置自变量将首先填充,而其他值将复制到数组中。

You can use a foreach loop to traverse the array inside the function. The function may have any positional arguments before the variable length argument. From the received values, the positional arguments will be populated first, leaving others to be copied to the array.

Example

<?php
   function myfunction($x, ...$numbers) {
      echo "First number: $x" . PHP_EOL;
      echo "Remaining numbers: ";
      foreach ($numbers as $n) {
         echo "$n  ";
      }
   }
   myfunction(5, 12, 9, 23, 8, 41);
?>

它将生成以下 output

It will produce the following output

First number: 5
Remaining numbers: 12  9  23  8  41

Variadic Functions

即使没有“…​”语法,也可以处理传递给函数的可变数量的自变量。PHP 具有内置的函数,例如 func_num_args()、func_get_arg() 和 func_get_args(),这些函数可以用类似的结果来使用。

It is possible to process a variable number of arguments to a function, even without the "…​" syntax. PHP has built_in functions like func_num_args(), func_get_arg() and func_get_args(), which can be used with similar result.

  1. func_num_args() − Returns the number of arguments passed to the function.

  2. func_get_arg() − Returns an item from the argument list

  3. func_get_args() − Returns an array comprising a function’s argument list

Example

上面可变参数的示例可以用以下函数重写:

The above example of variable arguments can be rewritten with these functions as below −

<?php
   function myfunction() {
      $sum = 0;
      foreach (func_get_args() as $n) {
         $sum += $n;
      }
      return $sum;
   }
   echo myfunction(5, 12, 9, 23, 8, 41);
?>

它将生成以下 output

It will produce the following output

98

Example

该程序输出传递给函数的所有数字:

This program prints all the numbers passed to the function −

<?php
   function myfunction() {
      $len = func_num_args();
      echo "Numbers : ";
      $i=0;
      for ($i=0; $i<$len; $i++)
      echo func_get_arg($i) . " ";
   }
   myfunction(5, 12, 9, 23, 8, 41);
?>

它将生成以下 output

It will produce the following output

Numbers : 5 12 9 23 8 41