Bevezetés a csereprogramba a PHP-ben

Ebben a cikkben megtanuljuk a számok cseréjét a PHP-ben. Megtanuljuk, hogyan határozzuk meg a cserét, megtanuljuk, hogyan kell kódolni két szám cseréjét, hogyan kell kicserélni több mint két számot és három számot, és hogyan lehet cserélni számokat ideiglenes változókkal vagy anélkül és még sok más.

Először kezdjük a meghatározással.

"A csere a PHP-ben az értékek cseréjeként definiált kifejezés."

Két szám cseréje két érték cseréjének folyamata ideiglenes változó használatával vagy anélkül. Remélem, hogy ezek a példák hasznosak minden programozó számára, akik meg akarják tanulni a csere fogalmát.

Hogyan cserélhetünk két számot a PHP-ben?

A számok cseréjének kétféle módja van. Ezek a számok numerikus értékeket tartalmaznak.

  • Két szám cseréje ideiglenes változóval.
  • Két szám cseréje ideiglenes változó nélkül.

Tegyük fel, hogy van egy változó, amelynek értéke 10,

szám 1 = 10;

A másik 20 értékű változóval

szám 2 = 20;

E két szám felcserélésekor az eredménynek a következőnek kell lennie:

szám1 = 20

szám2 = 10

Ez a harmadik átmeneti változó használatával és átmeneti változó nélkül is lehetséges. Két szám cseréje +, -, *, / operátorok segítségével történhet.

1. Két szám cseréje ideiglenes változóval

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

Teljesítmény :

2. Két szám cseréje ideiglenes változó nélkül

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

Kimenet:

3. Két szám cseréje olyan funkcióval, mint a lista () tömb ()

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

Kimenet:

Hogyan cserélhetünk három számot a PHP-ben?

A számok cseréjének kétféle módja van. Ezek a számok numerikus értékeket tartalmaznak.

  • Három szám cseréje ideiglenes változóval.
  • Három szám cseréje ideiglenes változó nélkül.

1. Három szám cseréje az ideiglenes változóval

Most, hogy megtanultuk két szám cseréjét, hasonló módon megtanuljuk három szám cseréjét is. A következő példa bemutatja, hogy egy ideiglenes (ideiglenes) változót használunk három szám cseréjére.

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

Kimenet:

2. Három szám cseréje ideiglenes változó használata nélkül

A logika itt az, hogy kiszámítja a teljes összeget, és hozzárendel egy $ num1 változóhoz.

És akkor,

kiszámítja a $ num1 értéket, rendelje hozzá ezt az értéket a $ num2 értékhez,

kiszámítja a $ num2 értékét, rendelje hozzá ezt az értéket a $ num3 értékhez,

kiszámítja a $ num3 értékét, és rendelje újra ezt az értéket a $ num1 értékhez.

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

Kimenet:

Következtetés

Remélem, hogy ez a cikk minden programozó számára hasznos, akik meg akarják tanulni a számok cseréjét. Ez a cikk mind két, mind három szám cseréjét tartalmazza, megfelelő példákkal. Ezek a példák, ha ezeket gyakorolják, elősegítik a koncepció megtanulását és a logika emlékezetét is.

Ajánlott cikkek

Ez a PHP csere útmutatója. Itt tárgyaljuk, hogyan lehet két vagy három számot cserélni ideiglenes változók felhasználásával vagy anélkül, példáikkal együtt. A következő cikkeket is megnézheti további információkért -

  1. Ülések a PHP-ben
  2. Minták a PHP-ben
  3. Cookie a PHP-ben
  4. Túlterhelés a PHP-ben
  5. Túlterhelés a Java-ban
  6. Python túlterhelés

Kategória: