搜尋

PHP 中的交換

Aug 29, 2024 pm 01:12 PM
php

在本文中,我們將學習 PHP 中的數位交換。我們將學習如何定義交換、如何編寫程式碼來交換兩個數字、如何交換兩個以上數字和三個數字以及如何在有或沒有臨時變數的情況下交換數字等等。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

讓我們先從定義開始。

「PHP 中的交換是一個定義為值交換的術語。」

交換兩個數字是使用或不使用臨時變數交換兩個值的過程。我希望這些例子對所有想要學習交換概念的程式設計師有所幫助。

如何在 PHP 中交換兩個數字?

交換號碼有兩種方法。這些數字包含數值。

  • 用臨時變數交換兩個數字。
  • 在沒有臨時變數的情況下交換兩個數字。

假設我們有一個變數的值為 10,

數字1 = 10;

另一個變數的值為 20,

數字2 = 20;

交換這兩個數字,結果應該是,

數字1 =20

數字2= 10

這可以透過使用第三個臨時變數來實現,也可以不使用臨時變數。可以使用 +、-、* 和 / 運算子交換兩個數字。

1.用臨時變數交換兩個數字

代碼:

<?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 中的交換

2.不使用臨時變數交換兩個數字

代碼:

<?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 中的交換

3.使用 list() 和 array() 等函數交換兩個數字

代碼:

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

輸出:

PHP 中的交換

如何在 PHP 中交換三個數字?

交換號碼有兩種方法。這些數字包含數值。

  • 用臨時變數交換三個數字。
  • 在沒有臨時變數的情況下交換三個數字。

1.使用臨時變數交換三個數字

現在我們已經學會了兩個數字的交換,我們已經學會了三個數字的交換。以下範例示範了臨時(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;
?>

輸出:

PHP 中的交換

2.不使用臨時變數交換三個數字

邏輯是計算總和並將其分配給 $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 中的交換

希望這篇文章對所有希望學習數位交換的程式設計師有所幫助。本文提供了兩個和三個數字的交換以及適當的範例。如果練習這些範例,將幫助您理解概念並幫助您記住邏輯。

以上是PHP 中的交換的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP:服務器端腳本語言的簡介PHP:服務器端腳本語言的簡介Apr 16, 2025 am 12:18 AM

PHP是一種服務器端腳本語言,用於動態網頁開發和服務器端應用程序。 1.PHP是一種解釋型語言,無需編譯,適合快速開發。 2.PHP代碼嵌入HTML中,易於網頁開發。 3.PHP處理服務器端邏輯,生成HTML輸出,支持用戶交互和數據處理。 4.PHP可與數據庫交互,處理表單提交,執行服務器端任務。

PHP和網絡:探索其長期影響PHP和網絡:探索其長期影響Apr 16, 2025 am 12:17 AM

PHP在過去幾十年中塑造了網絡,並將繼續在Web開發中扮演重要角色。 1)PHP起源於1994年,因其易用性和與MySQL的無縫集成成為開發者首選。 2)其核心功能包括生成動態內容和與數據庫的集成,使得網站能夠實時更新和個性化展示。 3)PHP的廣泛應用和生態系統推動了其長期影響,但也面臨版本更新和安全性挑戰。 4)近年來的性能改進,如PHP7的發布,使其能與現代語言競爭。 5)未來,PHP需應對容器化、微服務等新挑戰,但其靈活性和活躍社區使其具備適應能力。

為什麼要使用PHP?解釋的優點和好處為什麼要使用PHP?解釋的優點和好處Apr 16, 2025 am 12:16 AM

PHP的核心優勢包括易於學習、強大的web開發支持、豐富的庫和框架、高性能和可擴展性、跨平台兼容性以及成本效益高。 1)易於學習和使用,適合初學者;2)與web服務器集成好,支持多種數據庫;3)擁有如Laravel等強大框架;4)通過優化可實現高性能;5)支持多種操作系統;6)開源,降低開發成本。

揭穿神話:PHP真的是一種死語嗎?揭穿神話:PHP真的是一種死語嗎?Apr 16, 2025 am 12:15 AM

PHP沒有死。 1)PHP社區積極解決性能和安全問題,PHP7.x提升了性能。 2)PHP適合現代Web開發,廣泛用於大型網站。 3)PHP易學且服務器表現出色,但類型系統不如靜態語言嚴格。 4)PHP在內容管理和電商領域仍重要,生態系統不斷進化。 5)通過OPcache和APC等優化性能,使用OOP和設計模式提升代碼質量。

PHP與Python辯論:哪個更好?PHP與Python辯論:哪個更好?Apr 16, 2025 am 12:03 AM

PHP和Python各有優劣,選擇取決於項目需求。 1)PHP適合Web開發,易學,社區資源豐富,但語法不夠現代,性能和安全性需注意。 2)Python適用於數據科學和機器學習,語法簡潔,易學,但執行速度和內存管理有瓶頸。

PHP的目的:構建動態網站PHP的目的:構建動態網站Apr 15, 2025 am 12:18 AM

PHP用於構建動態網站,其核心功能包括:1.生成動態內容,通過與數據庫對接實時生成網頁;2.處理用戶交互和表單提交,驗證輸入並響應操作;3.管理會話和用戶認證,提供個性化體驗;4.優化性能和遵循最佳實踐,提升網站效率和安全性。

PHP:處理數據庫和服務器端邏輯PHP:處理數據庫和服務器端邏輯Apr 15, 2025 am 12:15 AM

PHP在數據庫操作和服務器端邏輯處理中使用MySQLi和PDO擴展進行數據庫交互,並通過會話管理等功能處理服務器端邏輯。 1)使用MySQLi或PDO連接數據庫,執行SQL查詢。 2)通過會話管理等功能處理HTTP請求和用戶狀態。 3)使用事務確保數據庫操作的原子性。 4)防止SQL注入,使用異常處理和關閉連接來調試。 5)通過索引和緩存優化性能,編寫可讀性高的代碼並進行錯誤處理。

您如何防止PHP中的SQL注入? (準備的陳述,PDO)您如何防止PHP中的SQL注入? (準備的陳述,PDO)Apr 15, 2025 am 12:15 AM

在PHP中使用預處理語句和PDO可以有效防範SQL注入攻擊。 1)使用PDO連接數據庫並設置錯誤模式。 2)通過prepare方法創建預處理語句,使用佔位符和execute方法傳遞數據。 3)處理查詢結果並確保代碼的安全性和性能。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具