PHP program to swap two numbers
Last Updated :
14 Aug, 2023
Integer values can be stored in a variable in PHP. It is easy to store and modify and swap these integer values using various mentioned approaches:
Using a temporary variable: The value of the first number is stored in the temporary variable. This value is then replaced by the value of the second number. The second number is then assigned the value of the temporary variable. The time complexity required to perform this operation is O(1). The space required is also approximately equivalent to O(1).
PHP
<?php
// Declaring both the numbers
$num1 = 9;
$num2 = 4;
print ("Number 1 original: " . $num1 . "</br>");
print ("Number 2 original: " . $num2 . "</br>");
// Assigning num1 value to temp variable
$temp_num = $num1;
// Assigning num1 value to num2
$num1 = $num2;
// Assigning num2 value to temp num
$num2 = $temp_num;
print ("Number 1 modified: " . $num1 . "</br>");
print ("Number 2 modified: " . $num2 . "</br>");
?>
Output:
Number 1 original: 9
Number 2 original: 4
Number 1 modified: 4
Number 2 modified: 9
Using ^ operator: The XOR operator can be used to swap numbers in PHP. However, this technique works only for the case of integers. The XOR operator application takes constant time complexity and space complexity.
PHP
<?php
// Declaring both the numbers
$num1 = 9;
$num2 = 4;
print ("Number 1 original: " . $num1 . "</br>");
print ("Number 2 original: " . $num2 . "</br>");
// Swapping numbers
$num1 ^= $num2 ^= $num1 ^= $num2;
print ("Number 1 modified: " . $num1 . "</br>");
print ("Number 2 modified: " . $num2 . "</br>");
?>
Output:
Number 1 original: 9
Number 2 original: 4
Number 1 modified: 4
Number 2 modified: 9
Using array() method: An array object is created using the arguments as swapped numbers, that is, the first argument is the second number and the second argument is the first number.
Syntax:
array(num2 , num1)
Example: This is then assigned to a list object using the list() method, storing numbers in order i.e. the first number followed by the second number. This swaps the numbers. However, this method works only for integers.
PHP
<?php
// Declaring both the numbers
$num1 = 9;
$num2 = 4;
print ("Number 1 original: " . $num1 . "</br>");
print ("Number 2 original: " . $num2 . "</br>");
// Swapping numbers
list($num1, $num2) = array($num2, $num1);
print ("Number 1 modified: " . $num1 . "</br>");
print ("Number 2 modified: " . $num2 . "</br>")
?>
Output:
Number 1 original: 9
Number 2 original: 4
Number 1 modified: 4
Number 2 modified: 9
Using arithmetic operations: The original numbers are swapped by using arithmetic operators like addition(+), subtraction(-), multiply(*), and division(/).
Example 1:
PHP
<?php
// Declaring both the numbers
$num1 = 9;
$num2 = 4;
print ("Number 1 original: " . $num1 . "</br>");
print ("Number 2 original: " . $num2 . "</br>");
// Using artithmetic operations
// Using addition and subtraction for swap
$num1 = $num1+$num2;
$num2 = $num1-$num2;
$num1 = $num1-$num2;
print ("Number 1 modified: " . $num1 . "</br>");
print ("Number 2 modified: " . $num2 . "</br>");
?>
Output:
Number 1 original: 9
Number 2 original: 4
Number 1 modified: 4
Number 2 modified: 9
Example 2:
PHP
<?php
// Declaring both the numbers
$num1 = 9;
$num2 = 4;
print ("Number 1 original: " . $num1 . "</br>");
print ("Number 2 original: " . $num2 . "</br>");
// Using artithmetic operations
// Using multiply, division for swap
$num1 = $num1*$num2;
$num2 = $num1/$num2;
$num1 = $num1/$num2;
print ("Number 1 modified: " . $num1 . "</br>");
print ("Number 2 modified: " . $num2 . "</br>");
?>
Output:
Number 1 original: 9
Number 2 original: 4
Number 1 modified: 4
Number 2 modified: 9
Similar Reads
Write a program to display Reverse of any number in PHP ? Write a program to reverse the digits of an integer. Examples : Input : num = 12345 Output: 54321 Input : num = 876 Output: 678 It can be achieved using Iterative way or Recursive Way Iterative Method: Algorithm: Input: num (1) Initialize rev_num = 0 (2) Loop while num > 0 (a) Multiply rev_num by
2 min read
Output of PHP programs | Set 3 Predict the output of below PHP programs: Question 1 PHP <?php $number = array(0, 1, one, two, three, 5); $num = preg_grep("/[0-5]/", $number); print_r($num); ?> Options: Array([0]=>0 [1]=>1 [2]=>one [3]=>two [4]=>three [5]=>5) Array([2]=>one [3]=>two [4]=>
3 min read
Javascript program to swap two numbers without using temporary variable To swap two numbers without using a temporary variable, we have multiple approaches. In this article, we are going to learn how to swap two numbers without using a temporary variable. Below are the approaches used to swap two numbers without using a temporary variable: Table of Content Using Arithme
6 min read
Program to swap two integer parameters using call by value & call by address in PHP ? Call by value: In call by value, we will send the value from the calling function to the called function. The values of the calling function arguments are directly copied to the corresponding arguments of the called function. If any modification is done on the arguments of the called function, it wi
3 min read
Program to print multiplication table of any number in PHP In this article, we will see how to print the multiplication table of any given number using PHP. To make the multiplication table, first, we get a number input from the user and then use for loop to display the multiplication table. We use HTML and PHP to display the multiplication table. The HTML
1 min read