{"id":427,"date":"2025-06-10T20:17:47","date_gmt":"2025-06-10T12:17:47","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/427.html"},"modified":"2025-06-10T20:17:47","modified_gmt":"2025-06-10T12:17:47","slug":"%e5%9c%a8php%e4%b8%ad%e4%bd%bf%e7%94%a8exit%e5%92%8cdie%e7%bb%93%e6%9e%84","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/427.html","title":{"rendered":"\u5728PHP\u4e2d\u4f7f\u7528\u2018exit\u2019\u548c\u2018die\u2019\u7ed3\u6784"},"content":{"rendered":"<p><body><\/p>\n<h2>&#20171;&#32461;<\/h2>\n<p>&#26377;&#25928;&#30340;&#38169;&#35823;&#22788;&#29702;&#26159;&#20219;&#20309;&#20581;&#22766;&#24212;&#29992;&#31243;&#24207;&#30340;&#20851;&#38190;&#26041;&#38754;&#12290;&#22312;PHP&#20013;&#65292;exit&#21644;die&#26500;&#36896;&#20989;&#25968;&#20316;&#20026;&#31435;&#21363;&#32456;&#27490;&#33050;&#26412;&#30340;&#24378;&#22823;&#19988;&#31616;&#21333;&#30340;&#24037;&#20855;&#12290;&#35753;&#25105;&#20204;&#28145;&#20837;&#25506;&#35752;&#36825;&#20123;&#26500;&#24314;&#22359;&#30340;&#24037;&#20316;&#26041;&#24335;&#65292;&#36890;&#36807;&#23454;&#38469;&#31034;&#20363;&#25552;&#39640;&#24744;&#30340;PHP&#32534;&#31243;&#25216;&#33021;&#12290;<\/p>\n<h2>&#22312;PHP&#20013;&#20351;&#29992;exit&#20989;&#25968;&#12290;<\/h2>\n<p>&#22312;PHP&#20013;&#65292;&ldquo;&#36864;&#20986;&rdquo;&#26500;&#36896;&#29992;&#20110;&#32456;&#27490;&#24403;&#21069;&#33050;&#26412;&#12290;&#23427;&#24120;&#29992;&#20110;&#22312;&#26576;&#20123;&#26465;&#20214;&#19979;&#20572;&#27490;&#33050;&#26412;&#25191;&#34892;&#65292;&#24182;&#21487;&#36873;&#22320;&#36755;&#20986;&#19968;&#26465;&#28040;&#24687;&#21518;&#36864;&#20986;&#12290;<\/p>\n<pre><code>&lt;?php\nif (!file_exists('important.txt')) {\n    exit('File not found.');\n}\n\/\/ The script will continue here if the file exists.\n?&gt;<\/code><\/pre>\n<p>&#38656;&#35201;&#27880;&#24847;&#30340;&#26159;&#65292;&ldquo;exit&rdquo;&#21487;&#20197;&#24102;&#25110;&#19981;&#24102;&#25324;&#21495;&#26469;&#20351;&#29992;&#12290;<\/p>\n<pre><code>&lt;?php\nexit;\nexit();\nexit 'This is a message.';\n?&gt;<\/code><\/pre>\n<h2>&#22312;PHP&#20013;&#20351;&#29992;&ldquo;die&rdquo;&#20250;&#23548;&#33268;&#31243;&#24207;&#31435;&#21363;&#32456;&#27490;&#24182;&#26174;&#31034;&#38169;&#35823;&#20449;&#24687;&#65292;&#36825;&#36890;&#24120;&#29992;&#20110;&#35843;&#35797;&#25110;&#36864;&#20986;&#26576;&#20123;&#25805;&#20316;&#12290;&#22914;&#26524;&#20320;&#38656;&#35201;&#23454;&#29616;&#31867;&#20284;&#30340;&#25928;&#26524;&#65292;&#22312;&#20854;&#20182;&#32534;&#31243;&#35821;&#35328;&#20013;&#21487;&#20197;&#20351;&#29992;&#35832;&#22914;exit()&#12289;return false;&#31561;&#26041;&#27861;&#26469;&#32467;&#26463;&#20989;&#25968;&#25110;&#31243;&#24207;&#30340;&#25191;&#34892;&#12290;<\/h2>\n<p>&ldquo;die&rdquo; &#31561;&#21516;&#20110; &ldquo;&#36864;&#20986;&rdquo;&#65292;&#34892;&#20026;&#23436;&#20840;&#19968;&#33268;&#12290;&#36890;&#24120;&#29992;&#20110;&#36935;&#21040;&#38169;&#35823;&#26102;&#32456;&#27490;&#33050;&#26412;&#12290;<\/p>\n<pre><code>&lt;?php\nif (!@mysql_connect($server, $username, $password)) {\n    die('Could not connect: ' . mysql_error());\n}\n\/\/ The script will exit if the connection fails.\n?&gt;<\/code><\/pre>\n<h2>&#36864;&#20986;&#29366;&#24577;&#20195;&#30721;<\/h2>\n<p>exit &#21644; die &#21487;&#20197;&#36890;&#36807;&#20256;&#36882;&#19968;&#20010;&#25972;&#25968;&#26469;&#34920;&#31034;&#36864;&#20986;&#29366;&#24577;&#30721;&#65292;&#36825;&#22312;&#21629;&#20196;&#34892;&#33050;&#26412;&#20013;&#38750;&#24120;&#26377;&#29992;&#12290;<\/p>\n<pre><code>&lt;?php\n\/\/ Exit with a status code\nexit(0); \/\/ Successful completion\nexit(1); \/\/ General error\n?&gt;<\/code><\/pre>\n<p>&#26368;&#20339;&#23454;&#36341;&#24314;&#35758;&#22312;&#32456;&#27490;&#21518;&#20351;&#29992;&#26631;&#20934;&#30340;&#36864;&#20986;&#29366;&#24577;&#30721;&#26469;&#20256;&#36798;&#29305;&#23450;&#21547;&#20041;&#12290;<\/p>\n<h2>&#25511;&#21046;&#32531;&#20914;&#21306;&#36755;&#20986;<\/h2>\n<p>&#22312;&#22788;&#29702;&#36755;&#20986;&#32531;&#20914;&#26102;&#65292;&#21487;&#20197;&#20351;&#29992;&ldquo;exit&rdquo;&#21644;&ldquo;die&rdquo;&#26469;&#22312;&#32467;&#26463;&#33050;&#26412;&#21069;&#21521;&#32531;&#20914;&#21306;&#25171;&#21360;&#19968;&#26465;&#28040;&#24687;&#12290;<\/p>\n<pre><code>&lt;?php\nob_start();\n\necho 'This will buffer.';\nexit('This will flush the buffer before exiting.');\n\nob_end_flush();\n?&gt;<\/code><\/pre>\n<h2>&#22312;Web&#19978;&#19979;&#25991;&#20013;&#20351;&#29992;<\/h2>\n<p>&#22312;Web&#19978;&#19979;&#25991;&#20013;&#65292;&#20351;&#29992;&ldquo;exit&rdquo;&#25110;&ldquo;die&rdquo;&#26469;&#21457;&#36865;&#21709;&#24212;&#22836;&#21644;&#20869;&#23481;&#20197;&#21450;&#20248;&#38597;&#22320;&#32456;&#27490;&#33050;&#26412;&#25191;&#34892;&#26159;&#19968;&#31181;&#24120;&#35265;&#20570;&#27861;&#12290;<\/p>\n<pre><code>&lt;?php\nif (!isset($_GET['id'])) {\n    header('HTTP\/1.1 400 Bad Request');\n    exit('Missing ID Parameter');\n}\n\n\/\/ Script will process the ID if present.\n?&gt;<\/code><\/pre>\n<p>&#35760;&#24471;&#22312;&#32456;&#27490;&#33050;&#26412;&#26102;&#65292;&#21153;&#24517;&#21457;&#36865;&#36866;&#24403;&#30340;HTTP&#29366;&#24577;&#30721;&#12290;<\/p>\n<h2>&#38598;&#25104;&#24322;&#24120;&#22788;&#29702;<\/h2>\n<p>&#36825;&#20123;&#26500;&#36896;&#20063;&#21487;&#20197;&#22312;&#33258;&#23450;&#20041;&#24322;&#24120;&#22788;&#29702;&#31243;&#24207;&#20013;&#20351;&#29992;&#65292;&#20174;&#32780;&#20445;&#30041;&#25429;&#33719;&#22359;&#30340;&#24178;&#20928;&#35821;&#20041;&#12290;<\/p>\n<pre><code>&lt;?php\nfunction myExceptionHandler($exception) {\n    echo 'Exception: ',  $exception-&gt;getMessage(), \"n\";\n    exit(1);\n}\n\nset_exception_handler('myExceptionHandler');\n\nthrow new Exception('Unhandled Exception');\n\/\/ This will invoke myExceptionHandler()\n?&gt;<\/code><\/pre>\n<p>&#36890;&#36807;&#24322;&#24120;&#22788;&#29702;&#65292;&#33050;&#26412;&#21363;&#20351;&#22312;&#24847;&#22806;&#20107;&#20214;&#21457;&#29983;&#26102;&#20063;&#33021;&#20248;&#38597;&#22320;&#32467;&#26463;&#12290;<\/p>\n<h2>&#23433;&#20840;&#24433;&#21709;<\/h2>\n<p>&#22312;&#25191;&#34892;&#23433;&#20840;&#26816;&#26597;&#21518;&#65292;&#20351;&#29992;&ldquo;exit&rdquo;&#25110;&ldquo;die&rdquo;&#36864;&#20986;&#33050;&#26412;&#21487;&#20197;&#38450;&#27490;&#22240;&#26816;&#26597;&#22833;&#36133;&#23548;&#33268;&#30340;&#32487;&#32493;&#25191;&#34892;&#65292;&#20174;&#32780;&#21487;&#33021;&#38459;&#27490;&#22810;&#31181;&#31867;&#22411;&#30340;&#28431;&#27934;&#12290;<\/p>\n<pre><code>&lt;?php\nif (!isValidUser($user)) {\n    exit('Unauthorized access.');\n}\n\n\/\/ Sensitive operations go here as the user is now validated.\n?&gt;<\/code><\/pre>\n<h2>&#24615;&#33021;&#24433;&#21709;<\/h2>\n<p>&#34429;&#28982;&ldquo;&#36864;&#20986;&rdquo;&#21644;&ldquo;&#27515;&#20129;&rdquo;&#36825;&#20004;&#20010;&#35789;&#24456;&#26377;&#29992;&#65292;&#20294;&#23427;&#20204;&#30340;&#20351;&#29992;&#24212;&#24403;&#35880;&#24910;&#12290;&#29305;&#21035;&#26159;&#24403;&#22312;&#25805;&#20316;&#36807;&#31243;&#20013;&#31361;&#28982;&#32456;&#27490;&#33050;&#26412;&#26102;&#65292;&#21487;&#33021;&#20250;&#23548;&#33268;&#36164;&#28304;&#65288;&#22914;&#25991;&#20214;&#21477;&#26564;&#25110;&#25968;&#25454;&#24211;&#36830;&#25509;&#65289;&#26410;&#20851;&#38381;&#65292;&#20174;&#32780;&#24341;&#21457;&#24615;&#33021;&#38382;&#39064;&#12290;<\/p>\n<h2>&#27979;&#35797;&#21644;&#35843;&#35797;<\/h2>\n<p>&#22312;&#35843;&#35797;&#36807;&#31243;&#20013;&#65292;&#20020;&#26102;&#20351;&#29992; &#8216;exit&#8217; &#25110; &#8216;die&#8217; &#21487;&#20197;&#24110;&#21161;&#35782;&#21035;&#20195;&#30721;&#25191;&#34892;&#21040;&#36798;&#30340;&#28857;&#65292;&#23613;&#31649;&#26356;&#39640;&#32423;&#21035;&#30340;&#35843;&#35797;&#25216;&#26415;&#25512;&#33616;&#29992;&#20110;&#26356;&#22797;&#26434;&#30340;&#24212;&#29992;&#12290;<\/p>\n<h2>&#39640;&#32423;&#29992;&#27861;&#65306;CLI &#33050;&#26412;<\/h2>\n<p>&#22312;&#21629;&#20196;&#34892;&#30028;&#38754;&#65288;CLI&#65289;&#20013;&#65292;&#36825;&#20123;&#26500;&#36896;&#21487;&#20197;&#22522;&#20110;&#33050;&#26412;&#21442;&#25968;&#25110;&#29615;&#22659;&#29366;&#24577;&#26469;&#26465;&#20214;&#20572;&#27490;&#25191;&#34892;&#65292;&#24182;&#36890;&#36807;&#26174;&#24335;&#21487;&#35265;&#30340;&#38169;&#35823;&#28040;&#24687;&#21644;&#29366;&#24577;&#20195;&#30721;&#21521;&#29992;&#25143;&#26174;&#31034;&#12290;<\/p>\n<pre><code>&lt;?php\nif ($argc !== 2) {\n    fwrite(STDERR, \"Usage: php script.php [arg]n\");\n    exit(1);\n}\n\n\/\/ The following code is executed when the correct arguments are passed.\n?&gt;<\/code><\/pre>\n<h2>&#32467;&#35770;<\/h2>\n<p>exit &#21644; die &#26500;&#36896;&#22312; PHP &#20013;&#25552;&#20379;&#20102;&#19968;&#31181;&#31616;&#27905;&#19988;&#24378;&#22823;&#30340;&#26041;&#27861;&#26469;&#21450;&#26102;&#32456;&#27490;&#33050;&#26412;&#25191;&#34892;&#12290;&#27491;&#30830;&#20351;&#29992;&#36825;&#20123;&#24037;&#20855;&#20197;&#30830;&#20445;&#24178;&#20928;&#30340;&#32467;&#26463;&#12289;&#33391;&#22909;&#30340;&#38169;&#35823;&#22788;&#29702;&#12289;&#36164;&#28304;&#31649;&#29702;&#20197;&#21450;&#21487;&#32500;&#25252;&#30340;&#20195;&#30721;&#32467;&#26500;&#33267;&#20851;&#37325;&#35201;&#12290;&#23613;&#31649;&#31616;&#21333;&#65292;&#20294;&#23427;&#20204;&#22312; PHP &#32534;&#31243;&#20013;&#30340;&#20316;&#29992;&#23545;&#20110; Web &#24212;&#29992;&#21644;&#21629;&#20196;&#34892;&#24212;&#29992;&#37117;&#26159;&#37325;&#35201;&#30340;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#20171;&#32461; &#26377;&#25928;&#30340;&#38169;&#35823;&#22788;&#29702;&#26159;&#20219;&#20309;&#20581;&#22766;&#24212;&#29992;&#31243;&#24207;&#30340;&#038;#20851..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-427","my1js","type-my1js","status-publish","hentry","my1js2nav-jcyy"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/427","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=427"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=427"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=427"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}