PHP Program to Swap two Numbers without using third variable



In this post I am going to describe the very basic PHP Program which tell us how to swap two numbers without using third variable. This program is very commonly asked in the interviews 7 out of 10 times this type of question are asked in the interviews and it can be very easily made.

<?php
if(isset($_POST['submit'])){
	$A= $_POST['A'];// (let the value of A be 10 and B be 20)
	$B= $_POST['B'];
	$A=$A+$B; //adding the value of both variable in A now A=30
	$B=$A-$B; // now the value of B  is 10
	$A=$A-$B; // now the value of A  is 20
	echo "Value of A is ".$A."<br>";
	echo "Value of B is ".$B;
	
}
?>
<html>
	<head>
		<title>Swipe</title>
	</head>
	<body>
		<form action="" method='POST'>
		<label for="number">Please Enter the value of A</label>
		<input type="text" name="A" value="">
		<br><br>
		<label for="number">Please Enter the value of B</label>
		<input type="text" name="B" value="">
		<br><br>
		
		<input type="submit" value="Done" name="submit">
		</form>
	</body>
</html>

Program Output:

Stay tuned for the PHP Magento, WordPress and Magento2 latest tutorial and updates, Hope you enjoyed reading, if you need the professional Magento Development / PHP we can help you, just Click on the Link and send me your requirements.

Please Like the Post on Facebook or Google+.