Swapping in PHP

王林
王林Original
2024-08-29 13:12:08895browse

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 Tests

Start 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.

How to Swap Two Numbers in PHP?

There are two ways to swap numbers. These numbers hold numerical values.

  • Swapping two numbers with a temporary variable.
  • Swapping two numbers without a temporary variable.

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.

1. Swapping Two Numbers with a Temporary Variable

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:

Swapping in PHP

2. Swapping Two Numbers without Temporary Variable

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:

Swapping in PHP

3. Swapping of two numbers using a function like list() with array()

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:

Swapping in PHP

How to Swap Three Numbers in PHP?

There are two ways to swap numbers. These numbers hold numerical values.

  • Swapping three numbers with a temporary variable.
  • Swapping three numbers without a temporary variable.

1. Swapping Three Numbers using Temporary Variable

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:

Swapping in PHP

2. Swapping Three Numbers Without Using a Temporary Variable

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:

Swapping in PHP

Conclusion – Swapping in PHP

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Star Patterns in PHPNext article:Star Patterns in PHP