PHP - Swap two numbers without using Temporary Variable
The value of two variables can be swapped without using any temporary variables. The method involves using operators like +, *, / and bitwise.
Method 1: Using + operator
In the example below, the + operator is used to swap the value of two variables x and y.
<?php function swap($x, $y) { echo "Before Swap.\n"; echo "x = $x \n"; echo "y = $y \n"; //Swap technique $x = $x + $y; $y = $x - $y; $x = $x - $y; echo "After Swap.\n"; echo "x = $x \n"; echo "y = $y \n"; } swap(10, 25); ?>
The above code will give the following output:
Before Swap. x = 10 y = 25 After Swap. x = 25 y = 10
Method 2: Using * operator
Like + operator, the * operator can also be used to swap the value of two variables x and y.
<?php function swap($x, $y) { echo "Before Swap.\n"; echo "x = $x \n"; echo "y = $y \n"; //Swap technique $x = $x * $y; $y = $x / $y; $x = $x / $y; echo "After Swap.\n"; echo "x = $x \n"; echo "y = $y \n"; } swap(10, 25); ?>
The above code will give the following output:
Before Swap. x = 10 y = 25 After Swap. x = 25 y = 10
Method 3: Using / operator
Similarly / operator can also be used to swap the value of two variables x and y.
<?php function swap($x, $y) { echo "Before Swap.\n"; echo "x = $x \n"; echo "y = $y \n"; //Swap technique $x = $x / $y; $y = $x * $y; $x = $y / $x; echo "After Swap.\n"; echo "x = $x \n"; echo "y = $y \n"; } swap(10, 25); ?>
The above code will give the following output:
Before Swap. x = 10 y = 25 After Swap. x = 25 y = 10
Method 4: Using bitwise operator
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.
<?php function swap($x, $y) { echo "Before Swap.\n"; echo "x = $x \n"; echo "y = $y \n"; //Swap technique $x = $x ^ $y; $y = $x ^ $y; $x = $x ^ $y; echo "After Swap.\n"; echo "x = $x \n"; echo "y = $y \n"; } swap(10, 25); ?>
The above code will give the following output:
Before Swap. x = 10 y = 25 After Swap. x = 25 y = 10
Disadvantages of using above methods
- The multiplication and division based approaches fail if the value of one of the variable is 0.
- The addition based approach may fail due to arithmetic overflow. If x and y are too large, operation performed on operands may result into out of range integer.
Recommended Pages
- PHP Program - To Check Prime Number
- PHP Program - Bubble Sort
- PHP Program - Selection Sort
- PHP Program - Maximum Subarray Sum
- PHP Program - Reverse digits of a given Integer
- PHP - Swap two numbers
- PHP Program - Fibonacci Sequence
- PHP Program - Insertion Sort
- PHP Program - Find Factorial of a Number
- PHP Program - Find HCF of Two Numbers
- PHP Program - Merge Sort
- PHP Program - Shell Sort
- Stack in PHP
- Queue in PHP
- PHP Program - Find LCM of Two Numbers
- PHP Program - To Check Armstrong Number
- PHP Program - Counting Sort
- PHP Program - Radix Sort
- PHP Program - Find Largest Number among Three Numbers
- PHP Program - Print Floyd's Triangle