{"id":430,"date":"2025-06-10T20:20:24","date_gmt":"2025-06-10T12:20:24","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/430.html"},"modified":"2025-06-10T20:20:24","modified_gmt":"2025-06-10T12:20:24","slug":"%e5%9c%a8php%e4%b8%ad%e4%bd%bf%e7%94%a8goto%e7%bb%93%e6%9e%84","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/430.html","title":{"rendered":"\u5728PHP\u4e2d\u4f7f\u7528\u201cgoto\u201d\u7ed3\u6784"},"content":{"rendered":"<p><body><\/p>\n<h2>&#20171;&#32461;<\/h2>\n<p>&#23545;&#19981;&#36215;&#65292;&#25105;&#19981;&#33021;&#29702;&#35299;&#36825;&#21477;&#35805;&#12290;<code>goto<\/code>&#22312;PHP&#20013;&#65292;&#25805;&#20316;&#31526;&#26159;&#19968;&#31181;&#22791;&#21463;&#20105;&#35758;&#30340;&#35821;&#35328;&#26500;&#36896;&#65292;&#26082;&#21487;&#20197;&#26377;&#29992;&#21448;&#21487;&#33021;&#23545;&#31243;&#24207;&#32467;&#26500;&#21644;&#21487;&#32500;&#25252;&#24615;&#20135;&#29983;&#28508;&#22312;&#21361;&#23475;&#65292;&#22914;&#26524;&#34987;&#19981;&#24403;&#20351;&#29992;&#12290;&#23427;&#20801;&#35768;&#33050;&#26412;&#36339;&#36716;&#21040;&#31243;&#24207;&#30340;&#21478;&#19968;&#20010;&#37096;&#20998;&#65292;&#20294;&#24212;&#35880;&#24910;&#20351;&#29992;&#12290;<\/p>\n<h2>&#22522;&#26412;&#29992;&#27861;&#30340;&#20351;&#29992;&#26041;&#27861;&#12290;<code>goto<\/code><\/h2>\n<p>&#22312;&#26412;&#36136;&#19978;&#65292;&#30340;<code>goto<\/code>&#36816;&#33829;&#21830;&#36890;&#36807;&#25351;&#23450;&#26631;&#31614;&#26469;&#36716;&#31227;&#25511;&#21046;&#21040;&#24744;&#30340;&#33050;&#26412;&#30340;&#21478;&#19968;&#37096;&#20998;&#12290;&#26631;&#31614;&#26159;&#19968;&#31181;&#31616;&#21333;&#30340;&#26631;&#35782;&#31526;&#65292;&#21518;&#38754;&#36319;&#30528;&#20882;&#21495;&#12290;<code>:<\/code>&#25265;&#27465;&#65292;&#25105;&#26080;&#27861;&#29702;&#35299;&#24744;&#30340;&#38382;&#39064;&#65292;&#35831;&#24744;&#37325;&#26032;&#25551;&#36848;&#19968;&#19979;&#65292;&#35874;&#35874;&#12290;<code>goto<\/code>&#35813;&#35821;&#21477;&#20250;&#20351;&#31243;&#24207;&#36339;&#36716;&#21040;&#26631;&#31614;&#23450;&#20041;&#30340;&#34892;&#12290;<\/p>\n<pre><code>&lt;?php\nif ($user_authenticated) {\n    goto welcome;\n}\necho 'Access Denied.';\nexit;\n\nwelcome:\necho 'Welcome, user!';\n?&gt;<\/code><\/pre>\n<p>&#22312;&#19978;&#36848;&#20363;&#23376;&#20013;&#65292;&#22914;&#26524;<code>$user_authenticated<\/code>&#30830;&#23454;&#65292;&#33050;&#26412;&#36339;&#21040;&#20102;&#26631;&#31614;&#19978;&#12290;<code>welcome<\/code>&#23427;&#20250;&#26174;&#31034;&#27426;&#36814;&#20449;&#24687;&#12290;<\/p>\n<h2>&#20351;&#29992;&#25511;&#21046;&#24490;&#29615;&#65288;Control Loop&#65289;<code>goto<\/code><\/h2>\n<p>&#34429;&#28982;&#36890;&#24120;<code>break<\/code>and<code>continue<\/code>&#23427;&#20204;&#29992;&#20110;&#25511;&#21046;&#22238;&#36335;&#12290;<code>goto<\/code>&#20063;&#21487;&#20197;&#25913;&#21464;&#24490;&#29615;&#25191;&#34892;&#30340;&#27969;&#31243;&#65306;<\/p>\n<pre><code>&lt;?php\nfor ($i = 0; $i &lt; 10; $i++) {\n    if ($i === 5) {\n        goto end_loop;\n    }\n    echo $i . 'n';\n}\n\nend_loop:\necho 'Loop ended prematurely at i = 5';\n?&gt;<\/code><\/pre>\n<p>&#36825;&#20250;&#25171;&#21360;&#20174;0&#21040;4&#30340;&#25968;&#23383;&#65292;&#28982;&#21518;&#22312;&#36339;&#20986;&#24490;&#29615;&#26102;&#25191;&#34892;&#12290;<code>$i<\/code>&#36798;&#21040;5&#65292;&#25171;&#21360;&#32467;&#26463;&#28040;&#24687;&#12290;<\/p>\n<h2>&#36991;&#20813;&#28145;&#23618;&#23884;&#22871;&#20351;&#29992;<code>goto<\/code><\/h2>\n<p><code>goto<\/code>&#21487;&#20197;&#36991;&#20813;&#26465;&#20214;&#35821;&#21477;&#21644;&#24490;&#29615;&#23884;&#22871;&#30340;&#28145;&#24230;&#21270;&#65292;&#20294;&#36807;&#24230;&#20351;&#29992;&#23427;&#21487;&#33021;&#20250;&#23548;&#33268;&ldquo;&#28151;&#20081;&#20195;&#30721;&rdquo;&#65292;&#20351;&#36923;&#36753;&#38590;&#20197;&#29702;&#35299;&#12290;&#20197;&#19979;&#26159;&#22914;&#20309;&#35880;&#24910;&#20351;&#29992;&#23427;&#30340;&#31034;&#20363;&#12290;<code>goto<\/code>&#65306;<\/p>\n<pre><code>&lt;?php\nfunction complex_function() {\n    if (!do_something()) {\n        goto error;\n    }\n    if (!do_something_else()) {\n        goto error;\n    }\n    \/\/ ...\n    return true;\n\n    error:\n    log_error();\n    return false;\n}\n?&gt;<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#20363;&#23376;&#20013;&#65292;<code>goto<\/code>&#29992;&#20110;&#36339;&#36716;&#21040;&#38169;&#35823;&#22788;&#29702;&#37096;&#20998;&#12290;<\/p>\n<h2>&#27491;&#22312;&#20351;&#29992;&#12290;<code>goto<\/code>&#20351;&#29992; switch-case&#12290;<\/h2>\n<p>&#22312;switch&#35821;&#21477;&#20013;&#26377;&#22810;&#20010;case&#26102;&#65292;<code>goto<\/code>&#21487;&#20197;&#20943;&#23569;&#37325;&#22797;&#65292;&#36890;&#36807;&#24341;&#23548;&#27969;&#31243;&#21040;&#20849;&#29992;&#30340;&#20195;&#30721;&#22359;&#65306;<\/p>\n<pre><code>&lt;?php\nswitch ($command) {\n    case 'start':\n    case 'restart':\n        prepare_system();\n        \/\/ fall-through intended\n    case 'run':\n        system_run();\n        break;\n    default:\n        echo 'Unrecognized command.';\n}\n\nfunction prepare_system() {\n    echo 'Preparing system...';\n    goto run;\n\n    run:\n    echo 'System is running!';\n}\n?&gt;<\/code><\/pre>\n<p>&#23545;&#19981;&#36215;&#65292;&#24744;&#30340;&#21477;&#23376;&#26377;&#35823;&#65292;&#35831;&#37325;&#26032;&#36755;&#20837;&#12290;<code>prepare_system<\/code>&#20989;&#25968;&#20351;&#29992;<code>goto<\/code>&#30452;&#25509;&#36339;&#21040;<code>run<\/code>&#22312;&#20934;&#22791;&#21518;&#36827;&#34892;&#26631;&#31614;&#22788;&#29702;&#12290;<\/p>\n<h2>&#20351;&#29992;<code>goto<\/code>&#23545;&#20110;&#38169;&#35823;&#22788;&#29702;&#12290;<\/h2>\n<p>&#22312;PHP&#20013;&#22788;&#29702;&#38169;&#35823;&#30340;&#19968;&#31181;&#32467;&#26500;&#21270;&#26041;&#27861;&#26159;&#20351;&#29992;&#24322;&#24120;&#12290;&#28982;&#32780;&#65292;<code>goto<\/code>&#20063;&#21487;&#20197;&#22312;&#31616;&#21270;&#38169;&#35823;&#22788;&#29702;&#31574;&#30053;&#20013;&#21457;&#25381;&#20316;&#29992;&#65306;<\/p>\n<pre><code>&lt;?php\n\/\/ Some code\nif (!do_critical_operation()) {\n    goto error;\n}\n\/\/ More code\nif (!do_another_critical_operation()) {\n    goto error;\n}\n\/\/ Even more code\n\nreturn_success();\n\nerror:\nhandle_error();\n?&gt;<\/code><\/pre>\n<p>&#27599;&#20010;&#20851;&#38190;&#25805;&#20316;&#37117;&#26377;&#21487;&#33021;&#36339;&#21040;<code>error<\/code>&#26631;&#31614;&#65292;&#35843;&#29992;&#38169;&#35823;&#22788;&#29702;&#22120;&#12290;<\/p>\n<h2>&#32467;&#35770;<\/h2>\n<p>&#23545;&#19981;&#36215;&#65292;&#25105;&#26080;&#27861;&#29702;&#35299;&#24744;&#30340;&#38382;&#39064;&#65292;&#35831;&#37325;&#26032;&#25551;&#36848;&#24744;&#24819;&#34920;&#36798;&#30340;&#20869;&#23481;&#12290;<code>goto<\/code>&#25805;&#20316;&#31526;&#22312;PHP&#20013;&#26082;&#26159;&#24378;&#22823;&#30340;&#24037;&#20855;&#65292;&#20063;&#26159;&#21452;&#20995;&#21073;&#12290;&#27491;&#22914;&#25152;&#28436;&#31034;&#30340;&#37027;&#26679;&#65292;&#23427;&#30830;&#23454;&#26377;&#20854;&#29992;&#36884;&#65292;&#20294;&#20063;&#20276;&#38543;&#30528;&#35686;&#21578;&#65306;&#36807;&#24230;&#20351;&#29992;&#25110;&#28389;&#29992;&#21487;&#33021;&#23548;&#33268;&#38590;&#20197;&#38405;&#35835;&#12289;&#32500;&#25252;&#21644;&#35843;&#35797;&#30340;&#28151;&#20081;&#20195;&#30721;&#12290;&#22987;&#32456;&#27491;&#30830;&#22320;&#20351;&#29992;&#25805;&#20316;&#31526;&#12290;<code>goto<\/code>&#35880;&#24910;&#19988;&#32771;&#34385;&#21608;&#21040;&#22320;&#20351;&#29992;&#65292;&#20197;&#20445;&#25345;&#20195;&#30721;&#24211;&#30340;&#20581;&#24247;&#29366;&#24577;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#20171;&#32461; &#23545;&#19981;&#36215;&#65292;&#25105;&#19981;&#33021;&#29702;&#35299;&#36825;&#21477;&#35805;&#12290;goto&#22312;PHP&#20013;&#65292;&#25805;..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-430","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\/430","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=430"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=430"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=430"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}