Swap two number generally is the process of interchange two variables with each other. Just suppose you have X whose value 10 there is another number Y with value of 20. Now if we swap these two number with each other then the X value will change to 20 and Y value will change to 10.
So here I am gonna explain with a simple PHP program.
So here I am gonna explain with a simple PHP program.
Swap Two Number Program :-
<?php $x=10; $y=20; echo "Value of x: $x</br>"; echo "Value of y: $y</br>"; $temp=$x; $x=$y; $y=$temp; echo "Value of x: $x</br>"; echo "Value of y: $y</br>"; ?>
Output :-
Value of x: 10 Value of y: 20 Value of x: 20 Value of y: 10
You can also do this by without using third variable as below -
Swap Two Number Without Using Third Variable Program :-
<?php $x=10; $y=20; echo "Value of x: $x</br>"; echo "Value of y: $y</br>"; $x=$x+$y; $y=$x-$y; $x=$x-$y; echo "Value of x: $x</br>"; echo "Value of y: $y</br>"; ?>
Output :-
Value of x: 10 Value of y: 20 Value of x: 20 Value of y: 10
Note: Only a member of this blog may post a comment.