Php 简明教程

PHP - Call by Reference

PHP 默认使用“按值调用”机制来将参数传递给函数。如果函数内的参数改变,这些改变不会反映在函数外部。要允许函数修改其参数,必须使用“按引用调用”机制。

PHP uses the "call by value" mechanism, by default, for passing arguments to a function. If the arguments within the function are changed, the changes do not reflect outside of the function. To allow a function to modify its arguments, the "call by reference" mechanism must be used.

在 PHP 中,一个引用变量充当原始或寄主变量的“别名”,以便它们两个可以读写单个值。换句话说,两个不同名称的变量可以访问相同的值,它们的行为就好像它们是同一个变量。

In PHP, a reference variable acts as an "alias" to the original or host variable so that both of them can read and write a single value. In other words, variables of two different names can access to the same value and they behave as if they are the same variable.

以下 PHP 脚本将帮助你理解什么是引用。在此, $var 是一个普通字符串变量。我们将 $var1 声明为 $var 的引用,为后者附加“&”符号。

The following PHP script will help in understanding what references are. Here, $var is a normal string variable. We declare $var1 as a reference to $var, append "&" symbol to the latter.

$var = "Hello";
$var1 = &$var;

当我们说 $var1$var 的别名或引用时,这意味着它值的任何更改也会更改 $var 的值,反之亦然。

When we say that $var1 is an alias or reference of $var, it means any change in its value will also change the value of $var, and vice versa.

Example

以下示例演示了 PHP 中“按引用调用”的工作方式 −

The following example demonstrates how "call by reference" works in PHP −

<?php
   $var = "Hello";
   $var1 = &$var;

   $var1 = "Hello World";
   echo "var=$var var1=$var1" . PHP_EOL;

   $var = "How are you?";
   echo "var=$var var1=$var1" . PHP_EOL;
?>

它将生成以下 output

It will produce the following output

var=Hello World var1=Hello World
var=How are you? var1=How are you?

Calling a PHP Function by Reference

要按引用调用函数,你需要声明由“&”符号作为前缀的名称形式参数。

To call a function by reference, you need to declare the formal arguments with name prefixed by "&" symbol.

function callref(&$arg1, &$arg2) {
   Statements;
}

对函数的调用就像“按值调用”方法一样。

The call to the function is just as in "call by value" method.

callref($x, $y);

当调用函数时, $arg1 成为 $x 的引用, $arg2 成为 $y 的引用。

When the function is invoked, $arg1 becomes a reference to $x and $arg2 becomes a reference to $y.

如果在函数体内部, $arg1$arg2 (或两者)的值改变,它还会导致 $x$y 的值改变。

If, inside the function body, the value of $arg1 or $arg2 (or both) changes, it also causes the values of $x and $y to change.

Example

让我们来看看以下示例 −

Let us have a look at the following example −

<?php
   function  change_name(&$nm) {
      echo "Initially the name is $nm" . PHP_EOL;
      $nm = $nm."_new";
      echo "This function changes the name to $nm" . PHP_EOL;
   }

   $name = "John";
   echo "My name is $name" . PHP_EOL;
   change_name($name);
   echo "My name now is $name" . PHP_EOL;
?>

变量 $name 传递给函数 change_name() 。引用变量 &$nm 成为它的引用变量。 $nm 的任何更改都会反映在函数外的 $name 中。

The variable $name is passed to the function change_name(). A reference variable &$nm becomes its reference variable. Any change in $nm is reflected in $name outside the function.

它将生成以下 output

It will produce the following output

My name is John
Initially the name is John
This function changes the name to John_new
My name now is John_new

Swapping Two Variables

在以下 PHP 代码中,我们按值传递参数来调用函数。该函数尝试交换它们的值。

In the following PHP code, we call a function by passing the arguments by value. The function attempts to swap their values.

在函数内部,它们的值发生变化,但这不反映在函数执行后的实际参数中。

Inside the function, their values are changed, but this swap doesn’t reflect in the actual arguments after the execution of the function.

当使用引用方式传递参数来调用相同函数时,交换效果也会反映在实际参数中。

When the same function is called by passing the arguments by reference, the swap effect is reflected in the actual arguments as well.

<?php
   function  swap_value($a, $b) {
      echo "Initial values a = $a b = $b \n";
      $c = $a; $a = $b; $b = $c;
      echo "Swapped values a = $a b = $b \n";
   }

   $x = 10; $y =20;
   echo "Actual arguments x = $x y = $y \n\n";

   swap_value($x, $y);
   echo "Actual arguments do not change after the function: \n";
   echo "x = $x y = $y \n\n";

   function  swap_ref(&$a, &$b) {
      echo "Initial values a = $a b = $b \n";
      $c = $a; $a = $b; $b = $c;
      echo "Swapped values a = $a b = $b \n";
   }

   swap_ref($x, $y);
   echo "Actual arguments get changed after the function: \n";
   echo "x = $x y = $y";
?>

它将生成以下 output

It will produce the following output

Actual arguments x = 10 y = 20

Initial values a = 10 b = 20
Swapped values a = 20 b = 10
Actual arguments do not change after the function:
x = 10 y = 20

Initial values a = 10 b = 20
Swapped values a = 20 b = 10
Actual arguments get changed after the function:
x = 20 y = 10

Return by Reference

除了 PHP 中的函数能按引用接受参数,它还能返回引用。要定义返回引用的函数,请在函数名称前添加“&”符号。

Just as a function in PHP can accept arguments by reference, it can also return a reference. To define a function that returns a reference, prefix the name of the function by "&" symbol.

Example

下面的代码显示了返回引用的函数示例。它返回 $x ,这是 myfunction() 内部的 local 静态变量。由于在其前添加了“&”符号, $a (存储返回值的变量)成为 &x 的引用。因此,对 $a 中的任何更改也会更改 $x 的值。

The following code shows the example of a function returning a reference. It returns $x, which is a local static variable inside myfunction(). Since "&" symbol is prepended to it, $a (the variable that stores the return value) becomes a reference to &x. As a result, any change in $a will also change the value of $x.

<?php
   function &myfunction(){
      static $x=10;
      echo "x Inside function: $x \n";
      return $x;
   }

   $a=&myfunction();
   echo "Returned by Reference: $a \n";
   $a=$a+10;
   $a=&myfunction();
?>

它将生成以下 output

It will produce the following output

x Inside function: 10
Returned by Reference: 10
x Inside function: 20