PHP Program to swap two numbers using temporary variable


February 5, 2022, Learn eTutorial
1423

The act of swapping two variables in computer programming refers to the exchange of the variables’ values. This is usually done with data stored in memory. Swaps are used to shift the placements of data in comparison sorts. Swap is a built-in function in several computer languages.

This program will swap the two numbers in the PHP scripting language. This program will print the >result after interchanging the values in the output screen in php language.

 

ALGORITHM

STEP 1: Declare two variables a and b with values assigned to them

STEP 2: Declare the third variable temp and copy the value of a to it.

STEP 3: copy value of b to a

STEP 4: Assign value of temp to a 

STEP 5: Display value of a and b

PHP Source Code

                                           <?php 
$a = 10; 
$b = 30; 
$temp = $a; 
$a = $b; 
$b = $temp; 
echo "after swapping "; 
echo "a =".$a." b=".$b; 
?> 
                                      

OUTPUT

after swapping a =30 b=10