虽然我有一个个人约定,当我想在预期和期望的时候结束脚本的执行时,我使用exit;。当我因为一些问题(无法连接到数据库,无法写入文件等)需要结束执行时,我使用die("Something went wrong.");来“杀死”脚本。
当我使用退出时:
header( "Location: http://www.example.com/" ); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit; // I would like to end now.
当我使用die时:
$data = file_get_contents( "file.txt" );
if( $data === false ) {
die( "Failure." ); // I don't want to end, but I can't continue. Die, script! Die!
}
do_something_important( $data );