How to swap two numbers without using a temporary variable in PHP ?

We have two variables (parameters) “x” & “y”, here we do swap of two variables without using a third variable (parameters).

1) Swap numbers using Arithmetic Operators –
In below example we use addition and subtraction operation to do swap of 2 numbers

In below example we use multiplication and division operation to do swap of 2 numbers

 

2) Swap numbers using Bitwise XOR Operators –

Note:
In multiplication, we can not able to use 0 (zero), because of it make multiplication result as zero.

Your comments are appreciated!

Till than Happy Coding 🙂

1 comment:

Leave a Reply

Your email address will not be published. Required fields are marked *