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

Google Advertisements
Download Projects

Google Adsense Advertisements

Buy 3 Year Old Domain, Tutorialday.com, SeosmoTools.com @$200

Buy This Ad Space @$50 per Month, Ad Size 300X250 Contact on: hitesh.xc@gmail.com WhatsApp no. 8882538826

Yahoo Advertisements