Introducere în schimbarea în PHP

În acest articol, vom învăța schimbul de numere în PHP. Vom învăța cum definim swapping, vom învăța cum să codăm pentru a schimba două numere, cum puteți schimba mai mult de două numere și trei numere și cum să schimbați numere cu sau fără variabile temporare și multe altele.

Să începem mai întâi cu definiția.

„Schimbarea în PHP este un termen definit ca schimb de valori.”

Schimbarea a două numere este un proces de schimb de două valori utilizând sau fără utilizarea unei variabile temporare. Sper ca aceste exemple să fie utile tuturor programatorilor care doresc să învețe conceptul de swapping.

Cum să schimbați două numere în PHP?

Există două modalități de a schimba numerele. Aceste numere dețin valori numerice.

  • Schimbarea a două numere cu o variabilă temporară.
  • Schimbarea a două numere fără o variabilă temporară.

Să presupunem că avem o variabilă care deține o valoare 10,

număr1 = 10;

Iar cealaltă variabilă care deține o valoare 20,

număr2 = 20;

La schimbarea acestor două numere, rezultatul ar trebui să fie:

număr1 = 20

număr2 = 10

Acest lucru este posibil cu utilizarea a treia variabilă temporară și fără o variabilă temporară. Schimbarea a două numere se poate face folosind operatorii +, -, *, /.

1. Schimbarea a două numere cu o variabilă temporară

Cod:

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

Rezultat :

2. Schimbarea a două numere fără variabilă temporară

Cod:

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

ieşire:

3. Schimbarea a două numere folosind o funcție ca listă () cu tablou ()

Cod:

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

ieşire:

Cum să schimbați trei numere în PHP?

Există două modalități de a schimba numerele. Aceste numere dețin valori numerice.

  • Schimbarea a trei numere cu o variabilă temporară.
  • Schimbarea a trei numere fără o variabilă temporară.

1. Schimbarea a trei numere folosind variabilă temporară

Acum că am învățat schimbarea a două numere, într-un mod similar învățăm schimbarea a trei numere acum. Următorul exemplu arată modul în care se folosește o variabilă temporară (temp) pentru a schimba trei numere.

Cod:

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

ieşire:

2. Schimbarea a trei numere fără a utiliza variabila temporară

Logica de aici este de a calcula suma totală și de a o atribui unei variabile $ num1.

Și apoi,

calculați valoarea $ num1, atribuiți această valoare la $ num2,

calculați valoarea de $ num2, atribuiți această valoare la $ num3,

calculați valoarea lui $ num3 și atribuiți din nou această valoare lui $ num1.

Cod:

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

ieşire:

Concluzie

Sper ca acest articol să fie util tuturor programatorilor care doresc să învețe schimbarea numerelor. Acest articol are ambele schimburi de două și trei numere, cu exemple adecvate. Aceste exemple, dacă sunt practicate, vă vor ajuta să învățați conceptul și vă vor ajuta să vă amintiți și logica.

Articole recomandate

Acesta este un ghid pentru Schimbarea în PHP. Aici vom discuta despre cum să schimbați două sau trei numere cu sau fără a utiliza variabile temporare împreună cu exemplele sale. De asemenea, puteți consulta următoarele articole pentru a afla mai multe-

  1. Ședințe în PHP
  2. Modele în PHP
  3. Cookie în PHP
  4. Supraîncărcarea în PHP
  5. Supraîncărcare în Java
  6. Supraîncărcarea Python