In this article, we will learn the swapping of numbers in PHP. We will learn how to define swapping, how to code to swap two numbers, how to swap more than two numbers and three numbers and how to swap numbers with or without temporary variables, and more.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Let us begin with the definition first.
“Swapping in PHP is a term defined as interchanging of values.”
Swapping of two numbers is a process to exchange two values using or without using a temporary variable. I hope these examples are helpful to all the programmers who want to learn the concept of swapping.
There are two ways to swap numbers. These numbers hold numerical values.
Suppose we have one variable holding a value of 10,
number1 = 10 ;
And the other variable holding a value of 20,
number2 = 20;
On swapping these two numbers, the result should be,
number1 =20
number2= 10
This is possible with the use of the third temporary variable and without a temporary variable also. Swapping two numbers can be done using the +, -, *, and / operators.
Code:
<?PHP // Example of swapping of two numbers using a temporary variable // Declaring two variables $num1 = 100; $num2 = 200; // using echo statement print the variables before swapping two numbers echo "<br>"."Before Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<hr>"; // declaring temporary variable to be zero $temp = 0; // performing swap of numbers $temp = $num1; $num1 = $num2; $num2 = $temp; //using the echo statement print the variables after swapping the numbers echo "<br>"."After Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; ?>
Output:
Code:
<?php // Example of swapping of two numbers without using a temporary variable // Declaring two variables $num1 = 100; $num2 = 200; // using echo statemnt print the variables before swapping two numbers echo "<br>"."Swap done without using temparory variable"; echo "<hr>"; echo "<br>"."Before Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<hr>"; // performing swap of numbers $num1 = $num1 - $num2; $num2 = $num1 + $num2; $num1 = $num2 - $num1; //using the echo statement print the variables after swapping the numbers echo "<br>"."After Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; ?>
Output:
Code:
<?php // Example of swapping of two numbers using list() with array() // Declaring two variables $num1 = 100; $num2 = 200; // using echo statement print the variables before swapping two numbers echo "<br>"."Swap done without using predefined functions"; echo "<hr>"; echo "<br>"."Before Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<hr>"; // performing swap of numbers list($num1, $num2) = array($num2, $num1); //using the echo statement print the variables after swapping the numbers echo "<br>"."After Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; ?>
Output:
There are two ways to swap numbers. These numbers hold numerical values.
Now that we have learned the swapping of two numbers, we have learned the swapping of three numbers. The following example demonstrates how a temporary(temp) variable swaps three numbers.
Code:
<?php // Example of swapping three numbers using temporary variable // Declaring three variables $num1 = 100; $num2 = 200; $num3 = 300; // using echo statement print the variables before swapping three numbers echo "<br>"."Swap done without using temporary variable"; echo "<hr>"; echo "<br>"."Before Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<br>"."Value of third number is = ". $num3; echo "<hr>"; // performing swap of numbers //assign first number the total of three numbers $temp = $num1; $num1 = $num2; $num2 = $num3; $num3 = $temp; //using the echo statement print the variables after swapping the numbers echo "<br>"."After Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<br>"."Value of third number is = ". $num3; ?>
Output:
The logic is calculating the total sum and assigning it to a $num1 variable.
And then,
calculate the value of $num1, assign this value to $num2,
calculate the value of $num2, assign this value to $num3,
calculate the value of $num3, and assign this value to $num1 again.
Code:
<?php // Declaring three variables $num1 = 100; $num2 = 200; $num3 = 300; // using echo statement print the variables before swapping three numbers echo "<br>"."Swap done without using temporary variable"; echo "<hr>"; echo "<br>"."Before Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<br>"."Value of third number is = ". $num3; echo "<hr>"; // performing swap of numbers //assign first number the total of three numbers $num1 = $num1 + $num2 + $num3; $num2 = $num1 - ($num2 + $num3); $num3 = $num1 - ($num2 + $num3); $num1 = $num1 - ($num2 + $num3); //using the echo statement print the variables after swapping the numbers echo "<br>"."After Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<br>"."Value of third number is = ". $num3; ?>
Output:
I hope this article is helpful to all the programmers who wish to learn the swapping of numbers. This article has both swappings of two and three numbers with appropriate examples. These examples, if practiced, will help you understand the concept and help you remember the logic as well.
The above is the detailed content of Swapping in PHP. For more information, please follow other related articles on the PHP Chinese website!