(PHP 4, PHP 5)
exit — 輸出一個消息并且退出當前腳本
說明
void exit ([ string$status
] )
void exit
( int $status
)
中止腳本的執行。 盡管調用了 exit(), Shutdown函數 以及 object destructors 總是會被執行。
參數
status
如果 status
是一個字符串,在退出之前該函數會打印
status
。
如果 status
是一個 integer,該值會作為退出狀態碼,并且不會被打印輸出。
退出狀態碼應該在范圍0至254,不應使用被PHP保留的退出狀態碼255。
狀態碼0用于成功中止程序。
Note:
PHP >= 4.2.0 當 status
是一個 integer,不會打印輸出。
返回值
沒有返回值。
范例
Example #1 exit() 例子
<?php
$filename = '/path/to/data-file';
$file = fopen($filename, 'r')
or exit("unable to open file ($filename)");
?>
Example #2 exit() 狀態碼例子
<?php
//exit program normally
exit;
exit();
exit(0);
//exit with an error code
exit(1);
exit(0376); //octal
?>
Example #3 無論如何,Shutdown函數與析構函數都會被執行
<?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.';
?>
以上例程會輸出:
Shutdown: shutdown() Destruct: Foo::__destruct()
注釋
Note: 因為是一個語言構造器而不是一個函數,不能被 可變函數 調用。
Note:
該語法結構等同于 die()。
參見
register_shutdown_function() - Register a function for execution on shutdown