就像
下面的代碼
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表示沒有錯誤).