Php 简明教程

PHP - Default Arguments

类似于支持命令式编程的大多数语言,PHP 中的函数可以有一个或多个具有默认值的参数。因此,可以调用这样的函数而无需向其传递任何值。如果没有任何要传递的值,函数会获取其默认值进行处理。如果函数调用确实提供了值,则默认值将被覆盖。

Like most of the languages that support imperative programming, a function in PHP may have one or more arguments that have a default value. As a result, such a function may be called without passing any value to it. If there is no value meant to be passed, the function will take its default value for processing. If the function call does provide a value, the default value will be overridden.

function fun($arg1 = val1, $arg2 = val2) {
   Statements;
}

可以采用不同方式调用这样的函数 −

Such a function can be called in different ways −

fun();			# Function will use defaults for both arguments
fun($x);		# Function passes $x to arg1 and uses default for arg2
fun($x, $y);	# Both arguments use the values passed

Example 1

在此,我们定义一个名为 greeting() 的函数,具有两个参数,两个参数都有 string 作为其默认值。我们通过传递一个字符串、两个字符串和无任何参数来调用它。

Here we define a function called greeting() with two arguments, both having string as their default values. We call it by passing one string, two strings and without any argument.

<?php
   function  greeting($arg1="Hello", $arg2="world") {
      echo $arg1 . " ". $arg2 . PHP_EOL;
   }

   greeting();
   greeting("Thank you");
   greeting("Welcome", "back");
   greeting("PHP");
?>

它将生成以下 output

It will produce the following output

Hello world
Thank you world
Welcome back
PHP world

Example 2

你可以定义一个仅具部分参数默认值且必须向其他参数传递值的函数。

You can define a function with only some of the arguments with default value, and the others to which the value must be passed.

<?php
   function  greeting($arg1, $arg2="World") {
      echo $arg1 . " ". $arg2 . PHP_EOL;
   }

   # greeting(); ## This will raise ArgumentCountError
   greeting("Thank you");
   greeting("Welcome", "back");
?>

它将生成以下 output

It will produce the following output

Thank you World
Welcome back

第一个调用(无参数)引发 ArgumentCountError ,因为你必须为第一个参数传递值。如果仅传递一个值,则它将被列表中的第一个参数使用。

The first call (without argument) raises ArgumentCountError because you must pass value for the first argument. If only one value is passed, it will be used by the first argument in the list.

不过,如果你在无默认值的参数之前使用 default 声明参数,则只有在同时为两个参数传递值的情况下才能调用这样的函数。无法出现第一个参数使用默认值、第二个参数使用传递值的情况。

However, if you declare arguments with default before arguments without defaults, such function can be only called if values for both are passed. You cannot have a situation where the first argument uses the default, and the second using the passed value.

greeting() 函数现在具有默认 $arg1 和无默认 $arg2 值。

The greeting() function now has $arg1 with default and $arg2 without any default value.

function  greeting($arg1="Hello", $arg2) {
   echo $arg1 . " ". $arg2 . PHP_EOL;
}

如果你传递一个字符串“PHP”−

If you pass a string "PHP" −

greeting("PHP");

意图在于将结果打印为“Hello PHP”,则会显示以下错误消息。

with the intension to print the result as "Hello PHP", the following error message will be displayed.

PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function
greeting(), 1 passed in hello.php on line 10 and exactly 2 expected

Example 3

让我们定义函数 percent() 来计算三门科目的成绩百分比。

Let’s define a function percent() that calculates the percentage of marks in three subjects.

假设每门科目的成绩为 100 分,则函数定义中的 $total 参数的默认值为 300。

Assuming that the marks in each subject are out of 100, the $total argument in the function definition is given a default value as 300.

<?php
   function  percent($p, $c, $m, $ttl=300) {
      $per = ($p+$c+$m)*100/$ttl;
      echo "Marks obtained: \n";
      echo "Physics = $p Chemistry = $c Maths = $m \n";
      echo "Percentage = $per \n";
   }
   percent(50, 60, 70);
?>

它将生成以下 output

It will produce the following output

Marks obtained:
Physics = 50 Chemistry = 60 Maths = 70
Percentage = 60

不过,如果每门科目的最高分数为 50,则你必须向此函数传递第四个值,否则将根据 300 而不是 150 计算百分比。

However, if the maximum marks in each subject is 50, then you must pass the fourth value to the function, otherwise the percentage will be calculated out of 300 instead of 150.

<?php
   function  percent($p, $c, $m, $ttl=300) {
      $per = ($p+$c+$m)*100/$ttl;
      echo "Marks obtained: \n";
      echo "Physics = $p Chemistry = $c Maths = $m \n";
      echo "Percentage = $per \n";
   }
   percent(30, 35, 40, 150);
?>

它将生成以下 output

It will produce the following output

Marks obtained:
Physics = 30 Chemistry = 35 Maths = 40
Percentage = 70