Uvod v izmenjavo v PHP

V tem članku se bomo naučili zamenjave števil v PHP. Spoznali bomo, kako definiramo zamenjavo, naučili se bomo, kako kodirati zamenjati dve številki, kako zamenjati več kot dve številki in tri številke in kako zamenjati številke z ali brez začasnih spremenljivk in še več.

Začnimo najprej z definicijo.

"Zamenjava v PHP je izraz, ki je opredeljen kot izmenjava vrednosti."

Zamenjava dveh števil je postopek za izmenjavo dveh vrednosti z uporabo začasne spremenljivke ali brez nje. Upam, da so ti primeri koristni vsem programerjem, ki se želijo naučiti koncepta zamenjave.

Kako zamenjati dve številki v PHP-ju?

Obstajata dva načina zamenjave številk. Te številke vsebujejo številčne vrednosti.

  • Zamenjava dveh številk z začasno spremenljivko.
  • Zamenjava dveh številk brez začasne spremenljivke.

Recimo, da imamo eno spremenljivko z vrednostjo 10,

številka1 = 10;

In druga spremenljivka z vrednostjo 20,

številka2 = 20;

Pri zamenjavi teh dveh številk bi moral biti rezultat,

število1 = 20

število2 = 10

To je mogoče z uporabo tretje začasne spremenljivke in tudi brez začasne spremenljivke. Zamenjavo dveh števil lahko izvedete z operaterji +, -, *, /.

1. Zamenjava dveh števil s začasno spremenljivko

Koda:

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

Izhod :

2. Zamenjava dveh številk brez začasne spremenljivke

Koda:

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

Izhod:

3. Zamenjava dveh števil z uporabo funkcije, kot je list (), z nizom ()

Koda:

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

Izhod:

Kako zamenjati tri številke v PHP-ju?

Obstajata dva načina zamenjave številk. Te številke vsebujejo številčne vrednosti.

  • Zamenjava treh številk z začasno spremenljivko.
  • Zamenjava treh številk brez začasne spremenljivke.

1. Zamenjava treh števil z uporabo začasne spremenljivke

Zdaj, ko smo se naučili zamenjave dveh števil, se na podoben način naučimo zamenjave treh števil. Naslednji primer prikazuje, kako se za izmenjavo treh števil uporablja začasna (temp) spremenljivka.

Koda:

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

Izhod:

2. Zamenjava treh številk brez uporabe začasne spremenljivke

Tukaj je logika izračunati skupno vsoto in jo dodeliti spremenljivki $ num1.

In potem,

izračunati vrednost $ num1, dodeliti to vrednost $ num2,

izračunati vrednost $ num2, dodeliti to vrednost $ num3,

izračunajte vrednost $ num3 in to vrednost dodelite zopet $ num1.

Koda:

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

Izhod:

Zaključek

Upam, da je ta članek koristen vsem programerjem, ki se želijo naučiti zamenjave števil. Ta članek vsebuje obe zamenjavi dveh in treh številk z ustreznimi primeri. Ti primeri, če jih izvajate, vam bodo pomagali pri učenju koncepta in tudi pri zapomnitvi logike.

Priporočeni članki

To je vodnik za zamenjavo v PHP. Tukaj razpravljamo, kako zamenjati dve ali tri številke z ali brez uporabe začasnih spremenljivk skupaj z njegovimi primeri. Če želite izvedeti več, si oglejte tudi naslednje članke -

  1. Seje v PHP
  2. Vzorci v PHP
  3. Piškotek v PHP
  4. Preobremenitev v PHP
  5. Preobremenitev na Javi
  6. Python preobremenitev