首頁  >  文章  >  後端開發  >  PHP退出

PHP退出

WBOY
WBOY原創
2024-08-29 13:10:35234瀏覽

每當需要終止當前腳本以及PHP 中的消息時,我們都會使用PHP 中的一個名為exit 函數的內建函數,雖然exit 函數用於終止當前腳本,但它不會中斷物件析構函數和關閉函數被執行,退出函數採用一個參數,即訊息,其中訊息表示退出函數終止當前腳本期間要顯示的訊息,或者該訊息也可以是終止期間的狀態號透過exit 函數執行腳本。

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

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

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

文法:

exit(message)

哪裡,

該訊息表示在退出函數終止目前腳本期間要顯示的訊息,或者該訊息也可以是在退出函數終止腳本期間的狀態號。

PHP 中 exit 函數的工作原理

  • 每當需要終止目前腳本以及 PHP 中的消息時,我們都會使用 PHP 中名為 exit 函數的內建函數。
  • 雖然 exit 函數用於終止目前腳本,但它不會中斷物件析構函數並關閉函數的執行。
  • 退出函數有一個參數,就是訊息。
  • 參數message表示exit函數終止目前腳本時要顯示的訊息,或者該訊息也可以是exit函數終止腳本時的狀態號。

PHP 退出範例

讓我們討論幾個例子。

範例#1

PHP 程式來說明退出函數的工作原理,我們嘗試透過 PHP 程式連接到網站,如果無法連接,則使用退出函數退出腳本並顯示錯誤訊息:

代碼:

<html>
<body>
<?php
#a variable called website is used to store the URL of the website that is to be connected.
$website = "https://www.google.com/";
#fopen function is used in read mode to read the contents of the website
fopen($website,"r")
#in case if the program is unable to connect to the website and read the contents of the website, the current script is terminated along with an error message using exit function
or exit("Unable to connect to the website: $website ");
?>
</body>
</html>

輸出:

PHP退出

在上面的程式中,使用了一個名為 website 的變數來儲存要連接的網站的 URL。然後使用fopen函數以read模式開啟網站,並讀取網站內容。如果程式無法連接網站或失敗

讀取網站的內容,然後在使用退出函數終止腳本時顯示錯誤訊息,該函數在螢幕上顯示為輸出。輸出如上面的快照所示。

範例#2

PHP 程式來說明退出函數的工作原理,我們嘗試透過 PHP 程式連接到網站,如果無法連接,則使用退出函數退出腳本並顯示錯誤訊息:

代碼:

<html>
<body>
<?php
#a variable called website is used to store the URL of the website that is to be connected.
$website = "https://www.facebook.com/";
#fopen function is used in read mode to read the contents of the website
fopen($website,"r")
#in case if the program is unable to connect to the website and read the contents of the website, the current script is terminated along with an error message using exit function
or exit("Unable to connect to the website: $website ");
?>
</body>
</html>

輸出:

PHP退出

在上面的程式中,使用了一個名為 website 的變數來儲存要連接的網站的 URL。然後使用fopen函數以read模式開啟網站,並讀取網站內容。如果程式無法連線到網站或無法讀取網站內容,則會在使用退出函數終止腳本時顯示錯誤訊息,該函數在螢幕上顯示為輸出。

輸出如上面的快照所示。

範例#3

PHP 程式來說明退出函數的工作原理,我們嘗試透過 PHP 程式指定文件位置的路徑來開啟文件,如果無法開啟文件,則退出腳本並顯示錯誤訊息退出功能的使用:

代碼:

<html>
<body>
<?php
#a variable called filepath is used to store the path to the location of the file that is to be opened.
$filepath = "C:/Users/admin/Desktop/check";
#fopen function is used in read mode to read the contents of the file present at the location specified
fopen($filepath, "r")
#in case if the program is unable to open the file present at the location specified and read the contents of the file, the current script is terminated along with an error message using exit function
or exit("Unable to open the file present at the location $filepath");
?>
</body>
</html>

輸出:

PHP退出

在上面的程式中,使用了一個名為 filepath 的變數來儲存要開啟的檔案位置的路徑。然後使用fopen函數以讀取模式開啟文件,從指定文件位置的路徑讀取文件內容。如果程式無法開啟文件路徑指定位置處的文件,程式將透過使用退出函數來終止顯示錯誤訊息,該函數在螢幕上顯示為輸出。輸出如上面的快照所示。

結論

在本文中,我們透過程式設計範例了解了 PHP 中退出函數的定義、語法、退出函數的工作原理及其輸出。

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

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn