在php中使用exit(0)的方法聲明退出狀態(成功退出還是由於某情況意外終止),這樣做有什麼好處?這與直接退出有什麼區別

來源:互聯網
上載者:User
就像
下面的代碼

echo '配置錯誤';exit(3);  //狀態3表示由於配置錯誤而退出

// 直接退出exit('配置錯誤');

有什麼區別?

虛心向各位大神請教一二

回複內容:

就像
下面的代碼

echo '配置錯誤';exit(3);  //狀態3表示由於配置錯誤而退出

// 直接退出exit('配置錯誤');

有什麼區別?

虛心向各位大神請教一二

先給出結論: 有細微區別。

講道理還是拿文檔說事:

If status is a string, this function prints the status just before exiting.

If status is an integer, that value will be used as the exit status and not printed. Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.
Note: PHP >= 4.2.0 does NOT print the status if it is an integer.

簡單點說就是: 如果是字串,就會列印出來,如果是數字,就會作為退出的狀態代碼,不會被列印。

分別執行上面兩行代碼,很顯然可以看出結果是一樣的。

區別在於:
注釋第二行,在終端執行下面命令:
php test.php // 列印"出錯"
echo $? //列印 3

注釋第一行,在終端執行下面命令:
php test.php // 列印"出錯"
echo $? //列印 0

也就是說,eixt()當參數為int類型的時候,會作為退出狀態代碼。
$?解釋:Stores the exit value of the last command that was executed(最後一條命令的退出狀態,0表示沒有錯誤).

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.