{"id":450,"date":"2025-06-10T20:36:56","date_gmt":"2025-06-10T12:36:56","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/450.html"},"modified":"2025-06-10T20:36:56","modified_gmt":"2025-06-10T12:36:56","slug":"php-require-require_once-include-include_once","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/450.html","title":{"rendered":"PHP: require(), require_once(), include(), include_once()"},"content":{"rendered":"<p><body><\/p>\n<h2>&#20171;&#32461;<\/h2>\n<p>&#29702;&#35299;&#22914;&#20309;&#22312;PHP&#33050;&#26412;&#20013;&#21253;&#21547;&#22806;&#37096;&#25991;&#20214;&#23545;&#20110;&#27169;&#22359;&#21270;&#32534;&#31243;&#33267;&#20851;&#37325;&#35201;&#12290;PHP&#20989;&#25968;require(), require_once(), include(),&#21644;include_once()&#22312;&#27492;&#26041;&#38754;&#38750;&#24120;&#37325;&#35201;&#65292;&#20294;&#30693;&#36947;&#20309;&#26102;&#20197;&#21450;&#22914;&#20309;&#20351;&#29992;&#23427;&#20204;&#21487;&#33021;&#20250;&#35753;&#20154;&#22256;&#24785;&#12290;&#26412;&#25991;&#23558;&#35299;&#37322;&#23427;&#20204;&#30340;&#29992;&#27861;&#12289;&#24046;&#24322;&#20197;&#21450;&#26368;&#20339;&#23454;&#36341;&#65292;&#24182;&#25552;&#20379;&#30456;&#20851;&#20195;&#30721;&#31034;&#20363;&#12290;<\/p>\n<h2>&#29702;&#35299;include()<\/h2>\n<p>&#22312;PHP&#20013;&#65292;include()&#20989;&#25968;&#29992;&#20110;&#20174;&#19968;&#20010;PHP&#25991;&#20214;&#20013;&#25289;&#21462;&#20869;&#23481;&#65292;&#28982;&#21518;&#22312;&#26381;&#21153;&#22120;&#25191;&#34892;&#20043;&#21069;&#36827;&#34892;&#22788;&#29702;&#12290;&#36890;&#24120;&#24773;&#20917;&#19979;&#65292;&#34987;&#21253;&#21547;&#30340;&#20869;&#23481;&#23545;&#33050;&#26412;&#30340;&#36816;&#34892;&#24182;&#19981;&#26159;&#24517;&#38656;&#30340;&#12290;<\/p>\n<pre><code>&lt;?php\n\/\/ Include the file\ntitle.php;\necho 'The rest of the code.';\n?&gt;\n<\/code><\/pre>\n<p>&#22914;&#26524;title.php&#25991;&#20214;&#19981;&#23384;&#22312;&#25110;&#26377;&#38169;&#35823;&#65292;&#33050;&#26412;&#20250;&#21457;&#20986;&#35686;&#21578;&#24182;&#32487;&#32493;&#25191;&#34892;&#12290;<\/p>\n<h2>require() &#26159;&#19968;&#20010;&#29992;&#20110;&#21152;&#36733;JavaScript&#27169;&#22359;&#30340;&#20989;&#25968;&#65292;&#36890;&#24120;&#22312;Node.js&#29615;&#22659;&#20013;&#20351;&#29992;&#12290;<\/h2>\n<p>&#19982;include()&#20989;&#25968;&#31867;&#20284;&#65292;require()&#20989;&#25968;&#20250;&#20174;&#25351;&#23450;&#30340;&#25991;&#20214;&#20013;&#24341;&#20837;&#20869;&#23481;&#21040;&#24403;&#21069;&#25991;&#20214;&#20013;&#12290;&#20294;&#26159;&#65292;&#22914;&#26524;&#30446;&#26631;&#25991;&#20214;&#19981;&#23384;&#22312;&#25110;&#21253;&#21547;&#38169;&#35823;&#65292;&#33050;&#26412;&#23558;&#20250;&#20572;&#27490;&#25191;&#34892;&#24182;&#20135;&#29983;&#33268;&#21629;&#38169;&#35823;&#12290;<\/p>\n<pre><code>&lt;?php\n\/\/ Require the file\ntitle.php;\necho 'The script ends here if title.php is missing.';\n?&gt;\n<\/code><\/pre>\n<h2>&#20351;&#29992; include_once()<\/h2>\n<p>include_once() &#20989;&#25968;&#26159;&#23545; include() &#20989;&#25968;&#30340;&#25193;&#23637;&#65292;&#30830;&#20445;&#25991;&#20214;&#21482;&#34987;&#21253;&#21547;&#19968;&#27425;&#65292;&#20174;&#32780;&#38450;&#27490;&#22914;&#26524;&#20195;&#30721;&#19981;&#23567;&#24515;&#22810;&#27425;&#35843;&#29992;&#23548;&#33268;&#37325;&#22797;&#20869;&#23481;&#12290;<\/p>\n<pre><code>&lt;?php\n\/\/ Using include_once\ntitle.php;\nntitle.php; \/\/ This will not include the file again\necho 'This will only include title.php once.';\n?&gt;\n<\/code><\/pre>\n<h2>require_once() &#20989;&#25968;<\/h2>\n<p>require_once() &#26159; require() &#30340;&#38480;&#21046;&#24615;&#21452;&#32990;&#32974;&#65292;&#19982; include_once() &#31867;&#20284;&#65292;&#30830;&#20445;&#25991;&#20214;&#21482;&#34987;&#21253;&#21547;&#19968;&#27425;&#12290;&#22914;&#26524;&#20320;&#38656;&#35201;&#20351;&#29992;&#37325;&#35201;&#30340;&#21151;&#33021;&#25110;&#31867;&#65292;&#21017;&#36890;&#24120;&#24314;&#35758;&#20351;&#29992; require_once()&#12290;<\/p>\n<pre><code>&lt;?php\n\/\/ Require the file only oncerequire_once('header.php');\n\n\/\/ Even if we try to require it again, it won't\nrequire_once('header.php');\necho 'The header.php file will only be required a single time.';\n?&gt;\n<\/code><\/pre>\n<h2>&#27604;&#36739;&#20363;&#23376;<\/h2>\n<p>&#20197;&#19979;&#26159;&#20960;&#20010;&#27604;&#36739;&#20195;&#30721;&#31034;&#20363;&#65292;&#20197;&#20415;&#26356;&#22909;&#22320;&#29702;&#35299;&#65306;<\/p>\n<pre><code>\/\/ Example with include\n&lt;?php\ninclude('navigation.php');\ninclude('navigation.php'); \/\/ This will include the navigation again\necho 'Navigation may be duplicated.';\n?&gt;\n\n\/\/ Example with include_once\n&lt;?php\ninclude_once('navigation.php');ninclude_once('navigation.php'); \/\/ No duplicate navigation\necho 'Navigation is included just once.';\n?&gt;\n\n\/\/ Example with require\n&lt;?php\nrequire('config.php');nrequire('config.php'); \/\/ The script will halt here if config.php is not found twice\necho 'If config.php is missing, this will not be displayed.';\n?&gt;\n\n\/\/ Example with require_once\n&lt;?php\nrequire_once('config.php');require_once('config.php'); \/\/ No error, but the file won't be required againnecho 'This ensures config.php is loaded just once without errors.';\n?&gt;\n<\/code><\/pre>\n<h2>&#26368;&#20339;&#23454;&#36341;<\/h2>\n<p>&#22312;&#25972;&#21512;&#22806;&#37096;PHP&#25991;&#20214;&#26102;&#65292;&#35831;&#36981;&#24490;&#20197;&#19979;&#26368;&#20339;&#23454;&#36341;&#65306;<\/p>\n<p>&#20351;&#29992;include()&#20989;&#25968;&#22788;&#29702;&#38750;&#24517;&#38656;&#36164;&#28304;&#65292;&#20363;&#22914;&#27169;&#26495;&#37096;&#20998;&#25110;&#33756;&#21333;&#25991;&#20214;&#65292;&#36825;&#20123;&#36164;&#28304;&#21487;&#33021;&#38656;&#35201;&#26356;&#25913;&#19988;&#19981;&#24517;&#35201;&#29992;&#20110;&#24744;&#30340;&#33050;&#26412;&#27491;&#24120;&#36816;&#34892;&#12290;<\/p>\n<p>&#20351;&#29992;require_once()&#20989;&#25968;&#23545;&#20851;&#38190;&#20381;&#36182;&#39033;&#65292;&#22914;&#37197;&#32622;&#25991;&#20214;&#12289;&#21151;&#33021;&#24211;&#25110;&#31867;&#23450;&#20041;&#36827;&#34892;&#22788;&#29702;&#65292;&#20197;&#36991;&#20813;&#37325;&#22797;&#21644;&#38169;&#35823;&#12290;<\/p>\n<h2>&#39640;&#32423;&#29992;&#27861;<\/h2>\n<p>&#39640;&#32423;&#20351;&#29992;&#36825;&#20123;&#21151;&#33021;&#21487;&#20197;&#21253;&#25324;&#26681;&#25454;&#21464;&#37327;&#25110;&#36923;&#36753;&#26465;&#20214;&#21160;&#24577;&#21253;&#21547;&#65306;<\/p>\n<pre><code>&lt;?php\n\/\/ Dynamic inclusion with include_once\n$page = 'about';ninclude_once($page . '.php');\necho 'The ' . $page . ' content has been included.';\n?&gt;\n<\/code><\/pre>\n<h2>&#24635;&#32467;<\/h2>\n<p>&#26412;&#25351;&#21335;&#35814;&#32454;&#20171;&#32461;&#20102;PHP&#30340;&#25991;&#20214;&#21253;&#21547;&#20989;&#25968;&#65306;require()&#12289;require_once()&#12289;include()&#21644;include_once()&#12290;&#27599;&#20010;&#20989;&#25968;&#37117;&#26377;&#20854;&#29305;&#23450;&#30340;&#21151;&#33021;&#65292;&#21487;&#20197;&#26681;&#25454;&#38656;&#35201;&#36873;&#25321;&#20351;&#29992;&#65292;&#20197;&#30830;&#20445;&#25991;&#20214;&#34987;&#27491;&#30830;&#22320;&#21253;&#25324;&#24182;&#19988;&#21482;&#34987;&#21253;&#25324;&#19968;&#27425;&#25110;&#22810;&#27425;&#12290;&#20102;&#35299;&#20309;&#26102;&#20309;&#22320;&#20351;&#29992;&#21738;&#20123;&#20989;&#25968;&#21487;&#20197;&#26174;&#33879;&#25552;&#39640;&#24744;&#30340;PHP&#24320;&#21457;&#25928;&#29575;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#20171;&#32461; &#29702;&#35299;&#22914;&#20309;&#22312;PHP&#33050;&#26412;&#20013;&#21253;&#21547;&#22806;&#37096;&#25991;&#20214;&#23545;&#20110;&#27169;&#038;#22..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-450","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\/450","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=450"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=450"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=450"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}