Home >>PHP Tutorial >How to swap two numbers in PHP

How to swap two numbers in PHP

How to swap two numbers in PHP

	//first number
	//second number
	//third is blank
	//now put x's values in $z
	//and Y's values into $x
	//again store $z in $y
	//Print the reversed Values
	echo "<p align='center'>Now First numebr is : ". $x ."<br/>";
	echo "and Second number is : ". $y."</p>";

<form method="post">
<table align="center">
		<td>Enter First number</td>
		<td><input type="text" name="fn"/></td>
		<td>Enter Second number</td>
		<td><input type="text" name="sn"/></td>
		<td colspan="2" align="center">
		<input type="submit" value="Swap Numbers" name="swap"/></td>

Swap two variables value without using third variable in php

//swap two numbers without using third variable

//add x and y, store in x i.e : 30

//subtract  10 from 30  i.e : 20 and store in y

//subtract 20 from 30 i.e : 10 and store in x

//now print the reversed values
echo "Now x contains : ". $x ."<br/>";
echo "and y contains : ". $y;


Without Using Third Variable but using Some Predefined Functions


$a = 10;

$b = 20;

list($a, $b) = array($b, $a);

echo  $a . " " . $b;


Popular Tutorials