Swap Two number Program in PHP


Prev Tutorial Next Tutorial

Swap Two number Program in PHP

Swap two number with each other means interchange their value. Suppose you have two number a and b a contain 10 and b contain value 20 after swap their value, a have 20 and b have 10.

Swap two number using third variable program in PHP

<?php

$a=10;
$b=20;

echo "Value of a: $a</br>";
echo "Value of b: $b</br>";

$temp=$a;
$a=$b;
$b=$temp;

echo "Value of a: $a</br>";
echo "Value of b: $b</br>";

?>

Output

	
Value of a: 10
Value of b: 20
Value of a: 20
Value of b: 10

Swap two number without using third variable program in PHP

<?php

$a=10;
$b=20;

echo "Value of a: $a</br>";
echo "Value of b: $b</br>";

$a=$a+$b;
$b=$a-$b;
$a=$a-$b;

echo "Value of a: $a</br>";
echo "Value of b: $b</br>";

?>

Output

	
Value of a: 10
Value of b: 20
Value of a: 20
Value of b: 10

Prev Tutorial Next Tutorial

Advertisements




Download Projects