(PHP 4, PHP 5, PHP 7, PHP 8)
exit — 輸出一個(gè)消息并且退出當前腳本
$status
= ?): void$status
): void中止腳本的執行。 盡管調用了 exit(), Shutdown函數 以及 object destructors 總是會(huì )被執行。
exit
是個(gè)語(yǔ)法結構,如果沒(méi)有 status
參數要傳入,可以省略圓括號。
status
如果 status
是一個(gè)字符串,在退出之前該函數會(huì )打印
status
。
如果 status
是一個(gè) int,該值會(huì )作為退出狀態(tài)碼,并且不會(huì )被打印輸出。
退出狀態(tài)碼應該在范圍0至254,不應使用被PHP保留的退出狀態(tài)碼255。
狀態(tài)碼0用于成功中止程序。
沒(méi)有返回值。
示例 #1 exit() 例子
<?php
$filename = '/path/to/data-file';
$file = fopen($filename, 'r')
or exit("unable to open file ($filename)");
?>
示例 #2 exit() 狀態(tài)碼例子
<?php
//exit program normally
exit;
exit();
exit(0);
//exit with an error code
exit(1);
exit(0376); //octal
?>
示例 #3 無(wú)論如何,Shutdown函數與析構函數都會(huì )被執行
<?php
class Foo
{
public function __destruct()
{
echo 'Destruct: ' . __METHOD__ . '()' . PHP_EOL;
}
}
function shutdown()
{
echo 'Shutdown: ' . __FUNCTION__ . '()' . PHP_EOL;
}
$foo = new Foo();
register_shutdown_function('shutdown');
exit();
echo 'This will not be output.';
?>
以上例程會(huì )輸出:
Shutdown: shutdown() Destruct: Foo::__destruct()