How to swap two numbers without using third variable in php?

This is a very general question of any interview in any programming language. But most of the developers not aware about the logic of this question. 

Below is the solution of it see -

$first = 10;
$second = 20;

echo "Before swapping the number is First=".$first." second=".$second.'<br>';

$first  = $first + $second;
$second = $first - $second;
$first  = $first - $second;

echo "After swapping the number is First=".$first." second=".$second;



Output :

Before swapping the number is First=10 second=20
After swapping the number is First=20 second=10


You like this post don't forgot to leave a comment.

Chears :)
Happy coding

2 Comments

Previous Post Next Post