Php 简明教程

PHP – Swapping Variables

PHP 没有提供任何内置函数用于交换或互换两个变量的值。然而,有几种技术可以用来完成交换。

PHP doesn’t provide any built-in function with which you can swap or interchange values of two variables. However, there are a few techniques which you can use to perform the swap.

最直接的方法之一是使用第三个变量作为临时的占位符来实现交换。按照特定顺序使用算术运算符也很有效。你也可以用二进制异或操作符用于交换。在本教程中,我们将用 PHP 来实现这些交换技术

One of the most straightforward approaches is to use a third variable as a temporary place holder to facilitate swapping. Using the arithmetic operators in a specific order also is very effective. You can also use the binary XOR operator for swapping purpose. In this chapter, we shall implement these swapping techniques in PHP

Temporary Variable

从逻辑上来说,这是最明显、最简单的方法。要交换 “a” 和 “b” 的值,请使用第三个变量 “c”。将 “a” 的值赋给 “c”,使用 “b” 的现有值覆盖 “a”,然后将 “b” 设置为存储在 “c” 中的 “a” 的早期值。

This is logically the most obvious and the simplest approach. To swap values of "a" and "b", use a third variable "c". Assign the value of "a" to "c", overwrite "a" with existing value of "b" and then set "b" to the earlier value of "a" that was stored in "c".

Example

请看以下示例:

Take a look at the following example −

<?php
   $a = 10;
   $b = 20;
   echo "Before swapping - \$a = $a, \$b = $b". PHP_EOL;
   $c = $a;
   $a = $b;
   $b = $c;
   echo "After swapping - \$a = $a, \$b = $b". PHP_EOL;
?>

它将生成以下 output

It will produce the following output

Before swapping - $a = 10, $b = 20
After swapping - $a = 20, $b = 10

Using addition (+) Operator

此解决方案利用了这样一个事实:从两个数字的和中减去一个数字会得到第二个数字。换句话说,“sum(a+b) – a” 等于 “b”,反之亦然。

This solution takes the advantage of the fact that subtracting a number from the sum of two numbers gives back the second number. In other words, "sum(a+b) – a" is equal to "b" and vice versa.

Example

让我们利用该属性来交换 “a” 和 “b” −

Let us take advantage of this property to swap "a" and "b" −

<?php
   $a = 10;
   $b = 20;
   echo "Before swapping - \$a = $a, \$b = $b". PHP_EOL;
   $a = $a + $b;
   $b = $a - $b;
   $a = $a - $b;
   echo "After swapping - \$a = $a, \$b = $b". PHP_EOL;
?>

它将生成以下 output

It will produce the following output

Before swapping - $a = 10, $b = 20
After swapping - $a = 20, $b = 10

你也可以像这样使用其他算术运算符来执行交换:减法 (-)、乘法 (*) 和除法 (/)。

You can also use the other arithmetic operators – subtraction (-), multiplication (*) and division (/) in a similar manner to perform swapping.

Using list() Function

PHP 中的 list() 函数将数组解压为单独的变量。这有助于我们实现两个变量之间的交换目标。为此,生成一个包含 “a” 和 “b” 的数组,然后将其解压到 “b” 和 “a” 变量中,以获取具有交换值的方法获取 “a” 和 “b”。

The list() function in PHP unpacks the array in separate variables. This helps in our objective of performing swap between two variables. To do that, build an array of "a" and "b", and then unpack it to "b" and "a" variables to obtain "a" and "b" with interchanged values.

Example

请看以下示例:

Take a look at the following example −

<?php
   $a = 10;
   $b = 20;
   echo "Before swapping - \$a = $a, \$b = $b". PHP_EOL;
   $arr = [$a, $b];
   list($b, $a) = $arr;
   echo "After swapping - \$a = $a, \$b = $b". PHP_EOL;
?>

它将生成以下 output

It will produce the following output

Before swapping - $a = 10, $b = 20
After swapping - $a = 20, $b = 10

Bitwise XOR

按位异或 (^) 运算符也可以用于交换两个变量 “x” 和 “y” 的值。如果两个操作数在相同位置上的一个位为 1,则返回 1,否则返回 0。

The bitwise XOR (^) operator can also be used to swap the value of two variables "x" and "y". It returns 1 when one of two bits at same position in both operands is 1, otherwise returns 0.

Example

请看以下示例:

Take a look at the following example −

<?php
   $a = 10;
   $b = 20;
   echo "Before swapping - \$a = $a, \$b = $b". PHP_EOL;
   $a = $a ^ $b;
   $b = $a ^ $b;
   $a = $a ^ $b;
   echo "After swapping - \$a = $a, \$b = $b". PHP_EOL;
?>

它将生成以下 output

It will produce the following output

Before swapping - $a = 10, $b = 20
After swapping - $a = 20, $b = 10