PHP Program to swap two numbers using temporary variable

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 PHP scripting language. This program will print the >result after interchanging the values in output  screen in php language.



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

STEP 2: Declare the third variable temp and copy 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

$a = 10; 
$b = 30; 
$temp = $a; 
$a = $b; 
$b = $temp; 
echo "after swapping "; 
echo "a =".$a." b=".$b; 


after swapping a =30 b=10