Введение в свопинг в PHP

В этой статье мы узнаем, как поменять числа в PHP. Мы узнаем, как мы определяем перестановку, мы узнаем, как кодировать, чтобы поменять местами два числа, как поменять местами более двух чисел и три числа и как поменять числа с временными переменными или без них и многое другое.

Давайте начнем с определения в первую очередь.

«Обмен в PHP - это термин, определяемый как обмен значениями».

Обмен двух чисел - это процесс обмена двумя значениями с использованием или без использования временной переменной. Я надеюсь, что эти примеры полезны для всех программистов, которые хотят изучить концепцию обмена.

Как поменять два числа в PHP?

Есть два способа поменять номера. Эти числа содержат числовые значения.

  • Обмен двух чисел с временной переменной.
  • Обмен двух чисел без временной переменной.

Предположим, у нас есть одна переменная, содержащая значение 10,

число 1 = 10;

А другая переменная, содержащая значение 20,

число2 = 20;

При обмене этими двумя числами результат должен быть,

число 1 = 20

число2 = 10

Это возможно с использованием третьей временной переменной и без временной переменной. Поменять местами два числа можно с помощью операторов +, -, *, /.

1. Обмен двух чисел с временной переменной

Код:

// 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 "
"."Before Swap";
echo “ ”;
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo “ ”;
// 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 "
"."After Swap";
echo “ ”;
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
?>
<_?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 "
"."Before Swap";
echo “ ”;
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo “ ”;
// 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 "
"."After Swap";
echo “ ”;
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
?>
// 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 "
"."Before Swap";
echo “ ”;
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo “ ”;
// 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 "
"."After Swap";
echo “ ”;
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
?>
// 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 "
"."Before Swap";
echo “ ”;
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo “ ”;
// 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 "
"."After Swap";
echo “ ”;
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
?>
// 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 "
"."Before Swap";
echo “ ”;
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo “ ”;
// 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 "
"."After Swap";
echo “ ”;
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
?>

Выход :

2. Обмен двух чисел без временной переменной

Код:

<_?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 "
"."Swap done without using temparory variable";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo " ";
// 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 "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
?>
// 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 "
"."Swap done without using temparory variable";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo " ";
// 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 "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
?>
// 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 "
"."Swap done without using temparory variable";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo " ";
// 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 "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
?>
// 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 "
"."Swap done without using temparory variable";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo " ";
// 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 "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
?>
// 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 "
"."Swap done without using temparory variable";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo " ";
// 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 "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
?>

Выход:

3. Перестановка двух чисел с помощью функции вроде list () с array ()

Код:

<_?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 "
"."Swap done without using predefined functions";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo " ";
// performing swap of numbers
list($num1, $num2) = array($num2, $num1);
//using the echo statement print the variables after swapping the numbers
echo "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
?>
// 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 "
"."Swap done without using predefined functions";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo " ";
// performing swap of numbers
list($num1, $num2) = array($num2, $num1);
//using the echo statement print the variables after swapping the numbers
echo "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
?>
// 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 "
"."Swap done without using predefined functions";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo " ";
// performing swap of numbers
list($num1, $num2) = array($num2, $num1);
//using the echo statement print the variables after swapping the numbers
echo "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
?>
// 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 "
"."Swap done without using predefined functions";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo " ";
// performing swap of numbers
list($num1, $num2) = array($num2, $num1);
//using the echo statement print the variables after swapping the numbers
echo "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
?>
// 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 "
"."Swap done without using predefined functions";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo " ";
// performing swap of numbers
list($num1, $num2) = array($num2, $num1);
//using the echo statement print the variables after swapping the numbers
echo "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
?>

Выход:

Как поменять три числа в PHP?

Есть два способа поменять номера. Эти числа содержат числовые значения.

  • Поменять местами три числа с временной переменной.
  • Замена трех чисел без временной переменной.

1. Обмен трех чисел с использованием временной переменной

Теперь, когда мы изучили обмен двух чисел, аналогичным образом мы теперь изучаем обмен трех чисел. В следующем примере демонстрируется, как временная (временная) переменная используется для замены трех чисел.

Код:

<_?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 "
"."Swap done without using temporary variable";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
echo " ";
// 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 "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
?>
// 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 "
"."Swap done without using temporary variable";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
echo " ";
// 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 "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
?>
// 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 "
"."Swap done without using temporary variable";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
echo " ";
// 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 "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
?>
// 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 "
"."Swap done without using temporary variable";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
echo " ";
// 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 "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
?>
// 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 "
"."Swap done without using temporary variable";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
echo " ";
// 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 "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
?>

Выход:

2. Замена трех чисел без использования временной переменной

Логика здесь в том, чтобы вычислить общую сумму и присвоить ее переменной $ num1.

А потом,

рассчитать значение $ num1, присвоить это значение $ num2,

рассчитать значение $ num2, присвоить это значение $ num3,

вычислите значение $ num3 и снова присвойте это значение $ num1.

Код:

<_?php
// Declaring three variables
$num1 = 100;
$num2 = 200;
$num3 = 300;
// using echo statement print the variables before swapping three numbers
echo "
"."Swap done without using temporary variable";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
echo " ";
// 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 "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
?>
// Declaring three variables
$num1 = 100;
$num2 = 200;
$num3 = 300;
// using echo statement print the variables before swapping three numbers
echo "
"."Swap done without using temporary variable";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
echo " ";
// 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 "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
?>
// Declaring three variables
$num1 = 100;
$num2 = 200;
$num3 = 300;
// using echo statement print the variables before swapping three numbers
echo "
"."Swap done without using temporary variable";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
echo " ";
// 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 "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
?>
// Declaring three variables
$num1 = 100;
$num2 = 200;
$num3 = 300;
// using echo statement print the variables before swapping three numbers
echo "
"."Swap done without using temporary variable";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
echo " ";
// 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 "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
?>
// Declaring three variables
$num1 = 100;
$num2 = 200;
$num3 = 300;
// using echo statement print the variables before swapping three numbers
echo "
"."Swap done without using temporary variable";
echo " ";
echo "
"."Before Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
echo " ";
// 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 "
"."After Swap";
echo " ";
echo "
"."Value of first number is = ". $num1;
echo "
"."Value of second number is = ". $num2;
echo "
"."Value of third number is = ". $num3;
?>

Выход:

Вывод

Надеюсь, эта статья будет полезна всем программистам, которые хотят научиться менять цифры. В этой статье есть два и три числа с соответствующими примерами. Эти примеры, если их практиковать, помогут вам выучить концепцию и вспомнить логику.

Рекомендуемые статьи

Это руководство по обмену в PHP. Здесь мы обсуждаем, как поменять местами два или три числа с использованием или без использования временных переменных, вместе с его примерами. Вы также можете посмотреть следующие статьи, чтобы узнать больше

  1. Сессии в PHP
  2. Шаблоны в PHP
  3. Cookie в PHP
  4. Перегрузка в PHP
  5. Перегрузка в Java
  6. Перегрузка Python