Open In App

PHP program to swap two numbers

Improve
Improve
Like Article
Like
Save
Share
Report

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


Last Updated : 14 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads