Úvod do swapování v PHP

V tomto článku se naučíme vyměňování čísel v PHP. Naučíme se, jak definujeme swapování, naučíme se kódovat pro výměnu dvou čísel, jak vyměňovat více než dvě čísla a tři čísla a jak vyměňovat čísla s dočasnými proměnnými nebo bez nich a další.

Začněme nejprve definicí.

„Zaměňování v PHP je termín definovaný jako výměna hodnot.“

Přepínání dvou čísel je proces výměny dvou hodnot pomocí nebo bez použití dočasné proměnné. Doufám, že tyto příklady jsou užitečné pro všechny programátory, kteří se chtějí naučit koncepci swapování.

Jak zaměnit dvě čísla v PHP?

Existují dva způsoby výměny čísel. Tato čísla obsahují číselné hodnoty.

  • Zaměňování dvou čísel dočasnou proměnnou.
  • Zaměňování dvou čísel bez dočasné proměnné.

Předpokládejme, že máme jednu proměnnou, která drží hodnotu 10,

číslo1 = 10;

A další proměnná drží hodnotu 20,

číslo 2 = 20;

Výsledkem výměny těchto dvou čísel by mělo být,

number1 = 20

number2 = 10

To je možné s použitím třetí dočasné proměnné a také bez dočasné proměnné. Zaměňování dvou čísel lze provést pomocí operátorů +, -, *, /.

1. Výměna dvou čísel dočasnou proměnnou

Kód:

// 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;
?>

Výstup :

2. Výměna dvou čísel bez dočasné proměnné

Kód:

<_?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;
?>

Výstup:

3. Zaměňování dvou čísel pomocí funkce jako list () s array ()

Kód:

<_?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;
?>

Výstup:

Jak zaměnit tři čísla v PHP?

Existují dva způsoby výměny čísel. Tato čísla obsahují číselné hodnoty.

  • Zaměňování tří čísel dočasnou proměnnou.
  • Zaměňování tří čísel bez dočasné proměnné.

1. Výměna tří čísel pomocí dočasné proměnné

Nyní, když jsme se naučili střídání dvou čísel, podobně se naučíme střídání tří čísel nyní. Následující příklad ukazuje, jak se dočasná (dočasná) proměnná používá k výměně tří čísel.

Kód:

<_?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;
?>

Výstup:

2. Zaměňování tří čísel bez použití dočasné proměnné

Logikou je vypočítat celkovou částku a přiřadit ji k proměnné $ num1.

A pak,

vypočítat hodnotu $ num1, přiřadit tuto hodnotu $ num2,

vypočítat hodnotu $ num2, přiřadit tuto hodnotu $ num3,

vypočítat hodnotu $ num3 a tuto hodnotu znovu přiřadit $ num1.

Kód:

<_?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;
?>

Výstup:

Závěr

Doufám, že tento článek je užitečný pro všechny programátory, kteří se chtějí naučit vyměňovat čísla. Tento článek obsahuje obě swapování dvou a tří čísel s příslušnými příklady. Tyto příklady, pokud jsou praktikovány, vám pomohou naučit se koncepci a také si pamatovat logiku.

Doporučené články

Toto je průvodce swapováním v PHP. Zde diskutujeme, jak zaměnit dvě nebo tři čísla za použití dočasných proměnných nebo bez nich spolu s jejich příklady. Další informace naleznete také v následujících článcích

  1. Relace v PHP
  2. Vzory v PHP
  3. Soubor cookie v PHP
  4. Přetížení v PHP
  5. Přetížení v Javě
  6. Přetížení Pythonu

Kategorie: