PHP Program to Swap Two Variables

In this post, We would love to share with you how to swap two variables without using a third and how to swap the two numbers 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>";
?>

Recommended PHP Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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