在本文中,我們將學習 PHP 中的數位交換。我們將學習如何定義交換、如何編寫程式碼來交換兩個數字、如何交換兩個以上數字和三個數字以及如何在有或沒有臨時變數的情況下交換數字等等。
廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
讓我們先從定義開始。
「PHP 中的交換是一個定義為值交換的術語。」
交換兩個數字是使用或不使用臨時變數交換兩個值的過程。我希望這些例子對所有想要學習交換概念的程式設計師有所幫助。
交換號碼有兩種方法。這些數字包含數值。
假設我們有一個變數的值為 10,
數字1 = 10;
另一個變數的值為 20,
數字2 = 20;
交換這兩個數字,結果應該是,
數字1 =20
數字2= 10
這可以透過使用第三個臨時變數來實現,也可以不使用臨時變數。可以使用 +、-、* 和 / 運算子交換兩個數字。
代碼:
<?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 "<br>"."Before Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<hr>"; // 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 "<br>"."After Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; ?>
輸出:
代碼:
<?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 "<br>"."Swap done without using temparory variable"; echo "<hr>"; echo "<br>"."Before Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<hr>"; // 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 "<br>"."After Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; ?>
輸出:
代碼:
<?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 "<br>"."Swap done without using predefined functions"; echo "<hr>"; echo "<br>"."Before Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<hr>"; // performing swap of numbers list($num1, $num2) = array($num2, $num1); //using the echo statement print the variables after swapping the numbers echo "<br>"."After Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; ?>
輸出:
交換號碼有兩種方法。這些數字包含數值。
現在我們已經學會了兩個數字的交換,我們已經學會了三個數字的交換。以下範例示範了臨時(temp)變數如何交換三個數字。
代碼:
<?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 "<br>"."Swap done without using temporary variable"; echo "<hr>"; echo "<br>"."Before Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<br>"."Value of third number is = ". $num3; echo "<hr>"; // 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 "<br>"."After Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<br>"."Value of third number is = ". $num3; ?>
輸出:
邏輯是計算總和並將其分配給 $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 "<br>"."Swap done without using temporary variable"; echo "<hr>"; echo "<br>"."Before Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<br>"."Value of third number is = ". $num3; echo "<hr>"; // 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 "<br>"."After Swap"; echo "<hr>"; echo "<br>"."Value of first number is = ". $num1; echo "<br>"."Value of second number is = ". $num2; echo "<br>"."Value of third number is = ". $num3; ?>
輸出:
希望這篇文章對所有希望學習數位交換的程式設計師有所幫助。本文提供了兩個和三個數字的交換以及適當的範例。如果練習這些範例,將幫助您理解概念並幫助您記住邏輯。
以上是PHP 中的交換的詳細內容。更多資訊請關注PHP中文網其他相關文章!