PHP Swap Two Variables | PHP Tutorial

PHP Swap Two Variables | PHP Tutorial

PHP swap two variables or numbers value. We would love to share with you how to PHP swap two variables without using third and how to swap the two variables or numbers value By using the third variable.

Swapping two numbers or variables

Two numbers can be swapped or interchanged. It means the first number value will become the second number value and the second number will become the first number value.

Suppose you have two variables first is $x and it contains value 5, means $x=5; and the second is $y and it contains value 10.

After swapping, $x contains value 10 and $y is contained value 5.

For example :-

x = 5, y = 10  

After swapping,  

x = 10, y = 5  

In this tutorial, we will learn two ways to swapping two variables

  • Swap two numbers or variables without using third variable in PHP
  • Swap two numbers or variables using third variable in PHP

Swap two numbers or variables without using third variable in PHP

Here we will create a PHP program to swap two numbers or variable value without using the third variable. Follow the below steps and swap two variables or numbers value without using third variable:-

  • First, Define two variables and assign value, which you want to swap
  • Second, you add two define variable value like (x+y) and assign the value to x
  • Third, now you minus first variable to the second variable like (x-y) and assign the value to y
  • Fourth, you minus first variable to a second variable like (x-y) and assign the value to x
  • Final, print the swapped variables (x and y)
<?php
    //define variable and assign value that you want to swap 
	$x=5;  

	//define variable and assign value that you want to swap
	$y=10; 

	//swap two variable value by using arithmetic operation in PHP

	//here you will plus x and y value and assing the value to x
	$x=$x+$y; 

	//here you will minus x to y and assin the value to y
	$y=$x-$y; 

	// here again minus x to y and assing the value to x
	$x=$x-$y;

	//print the swapped value
	echo "Value of x: $x</br>";  
	echo "Value of y: $y</br>";  
?>  

Swap two numbers or variables using third variable in PHP

Here we will create a PHP program to swap two numbers or variable value by using the third variable. Follow the below steps and swap two variables or numbers value by using third variable:-

  • First, Define two variables and assign value, which you want to swap
  • Second, you assign the value of x to z
  • Third, you assign the value of y to x
  • Fourth, you assign the value of z to y
  • Final, print the swapped variables (x and y)
<?php
       //define variable and assign value that you want to swap 
	$x=5;  

	//define variable and assign value that you want to swap
	$y=10; 

	//here you will assign value x to z
	$z = $x;  

	//here you will assign value of y to x
	$x = $y;  

	// here you will assing value of z to y
	$y = $z;  

	//print the swapped value
	echo "Value of x: $x</br>";  
	echo "Value of y: $y</br>";  
?>  

Leave a Reply

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