exit

(PHP 4, PHP 5, PHP 7, PHP 8)

exit輸出一個消息并且退出當(dāng)前腳本

說明

exit(string $status = ?): void
exit(int $status): void

中止腳本的執(zhí)行。 盡管調(diào)用了 exit(), Shutdown函數(shù) 以及 object destructors 總是會被執(zhí)行。

exit 是個語法結(jié)構(gòu),如果沒有 status 參數(shù)要傳入,可以省略圓括號。

參數(shù)

status

如果 status 是一個字符串,在退出之前該函數(shù)會打印 status 。

如果 status 是一個 int,該值會作為退出狀態(tài)碼,并且不會被打印輸出。 退出狀態(tài)碼應(yīng)該在范圍0至254,不應(yīng)使用被PHP保留的退出狀態(tài)碼255。 狀態(tài)碼0用于成功中止程序。

返回值

沒有返回值。

范例

示例 #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 無論如何,Shutdown函數(shù)與析構(gòu)函數(shù)都會被執(zhí)行

<?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()
 

注釋

注意: 因為是一個語言構(gòu)造器而不是一個函數(shù),不能被 可變函數(shù) 調(diào)用。

注意:

該語法結(jié)構(gòu)等同于 die()。

參見