Php 简明教程

PHP - Returning Values

PHP 函数的函数体中可以有一个可选的 return 语句作为其最后一个语句。PHP 中的大多数内置函数都返回一个特定值。例如,strlen() 函数返回字符串的长度。类似地,用户定义的函数也可以返回某个值。

A PHP function can have an optional return statement as the last statement in its function body. Most of the built-in functions in PHP return a certain value. For example the strlen() function returns the length of a string. Similarly, a user defined function can also return a certain value.

函数是一个独立、完整且可重复使用的语句块。调用时,它执行某个任务,并将程序控制权送回其被调用的位置,即使没有使用 return 语句也是如此。return 语句允许它将一个值与控制权一起带回调用环境。

A function is an independent, complete and reusable block of statements. When called, it performs a certain task and sends the program control back to position from where it was called even if return statement is not used. The return statement allows it to take a value, along with the control, back to the calling environment.

function foo($arg_1, $arg_2) {
   statements;
   return $retval;
}

函数可以返回任何类型的数据,包括标量变量、数组和对象。没有前面表达式的 return 关键字返回 null,并且等同于函数根本没有返回值。

A function may return any type of data, including scalar variables, arrays and objects. A return keyword without any expression in front of it returns null, and is equivalent to a function not having a return value at all.

函数返回的值可以存储在变量中,可以放入表达式中,或者如果出现在 print 或 echo 中,则显示在输出中。

The value returned by a function can be stored in a variable, can be put in an expression, or, if appearing inside print or echo, is displayed in the output.

$res = foo($x, $y);

它允许在程序中进一步使用函数的返回值。

It allows the returned value of a function to be used further in the program.

Example

让我们修改前一章中的 addition() 函数,以包含一个 return 语句来返回加法结果。

Let us modify the addition() function in the previous chapter to include a return statement to return the result of addition.

<?php
   function addition($first, $second) {
      $result = $first+$second;
      return $result;
   }

   $x=10;
   $y=20;
   $z = addition($x, $y);
   echo "First number: $x Second number: $y Addition: $z". PHP_EOL;
?>

它将生成以下 output

It will produce the following output

First number: 10 Second number: 20 Addition: 30

PHP 中的函数可以具有任意数量的参数,但只能返回一个值。该函数在首次遇到 return 语句时就返回调用环境,放弃函数体中其余的语句。

A function in PHP may have any number of arguments, but can return only one value. The function goes back to the calling environment as soon as it comes across a return statement for the first time, abandoning the rest of statements in the function body.

Example

如果您尝试在 return 语句中包含多个值,则会遇到以下 PHP 解析错误:

If you try to include more than one values in the return statement, a PHP parse error is encountered as below −

<?php
   function raiseto($x) {
      $sqr =  $x**2;
      $cub =  $x**3;
      return $sqr, $cub;
   }
   $a = 5;
   $val = raiseto($a);
?>

它将生成以下 output

It will produce the following output

PHP Parse error: syntax error, unexpected token ",", expecting ";"

Conditional Return

可以在不同的条件语句下执行多个 return 语句。

You can have multiple return statements executed under different conditional statements.

Example

在以下程序中, raiseto() 函数返回索引参数(分别是 2 或 3)的一个数的平方或立方。

In the following program, the raiseto() function returns either the square or the cube of a number of the index argument, that is either 2 or 3, respectively.

<?php
   function raiseto($x, $i) {
      if ($i == 2) {
         return $x**2;
      } elseif ($i==3) {
         return $x**3;
      }
   }
   $a = 5;
   $b = 2;
   $val = raiseto($a, $b);
   echo "$a raised to $b = $val" . PHP_EOL;

   $x = 7;
   $y = 3;
   echo "$x raised to $y = " . raiseto($x, $y) . PHP_EOL;
?>

它将生成以下 output

It will produce the following output

5 raised to 2 = 25
7 raised to 3 = 343

Return Multiple Values as Array

PHP 中的函数只能返回一个值。不过,这个一个值可以是多个值构成的数组。我们可以利用此功能一次返回一个数的平方和立方。

The function in PHP is capable of returning only a single value. However, that single value can be an array of more than one values. We can take the advantage of this feature to return the square as well as the cube of a number at once.

Example

请看以下示例:

Take a look at the following example −

<?php
   function raiseto($x){
      $sqr =  $x**2;
      $cub =  $x**3;
      $ret = ["sqr" => $sqr, "cub" => $cub];
      return $ret;
   }
   $a = 5;
   $val = raiseto($a);
   echo "Square of $a: " . $val["sqr"] . PHP_EOL;
   echo "Cube of $a: " . $val["cub"] . PHP_EOL;
?>

它将生成以下 output

It will produce the following output

Square of 5: 25
Cube of 5: 125