{"id":428,"date":"2025-06-10T20:18:21","date_gmt":"2025-06-10T12:18:21","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/428.html"},"modified":"2025-06-10T20:18:21","modified_gmt":"2025-06-10T12:18:21","slug":"php%ef%bc%9a%e5%a6%82%e4%bd%95%e4%bb%8e%e5%8f%a6%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6%e5%8c%85%e5%90%ab%e4%bb%a3%e7%a0%81","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/428.html","title":{"rendered":"PHP\uff1a\u5982\u4f55\u4ece\u53e6\u4e00\u4e2a\u6587\u4ef6\u5305\u542b\u4ee3\u7801"},"content":{"rendered":"<p><body><\/p>\n<h2>&#20171;&#32461;<\/h2>\n<p>&#22312;PHP&#20013;&#65292;&#20174;&#21478;&#19968;&#20010;&#25991;&#20214;&#21253;&#21547;&#20195;&#30721;&#26159;&#19968;&#31181;&#24120;&#35265;&#30340;&#23454;&#36341;&#65292;&#26377;&#21161;&#20110;&#23558;&#20195;&#30721;&#32452;&#32455;&#20026;&#21487;&#37325;&#29992;&#30340;&#37096;&#20998;&#12290;&#36825;&#19981;&#20165;&#20351;&#20195;&#30721;&#31649;&#29702;&#26356;&#21152;&#31616;&#21333;&#65292;&#32780;&#19988;&#26377;&#21161;&#20110;&#21019;&#24314;&#26356;&#26131;&#20110;&#32500;&#25252;&#30340;&#20195;&#30721;&#24211;&#12290;&#22312;&#36825;&#31687;&#25945;&#31243;&#20013;&#65292;&#24744;&#23558;&#23398;&#20064;&#20351;&#29992;include()&#12289;include_once()&#12289;require()&#21644;require_once()&#35821;&#21477;&#30340;&#21508;&#31181;&#26041;&#27861;&#26469;&#20174;&#20854;&#20182;&#25991;&#20214;&#20013;&#21253;&#21547;PHP&#20195;&#30721;&#12290;<\/p>\n<h2>include()&#30340;&#22522;&#26412;&#29992;&#27861;&#12290;<\/h2>\n<p>include()&#35821;&#21477;&#29992;&#20110;&#20174;&#25351;&#23450;&#30340;&#25991;&#20214;&#20013;&#22797;&#21046;&#25152;&#26377;&#25991;&#26412;\/&#20195;&#30721;\/&#26631;&#35760;&#21040;&#20351;&#29992;&#35813;&#35821;&#21477;&#30340;&#25991;&#20214;&#20013;&#12290;&#36825;&#23545;&#20110;&#21253;&#25324;&#22914;&#22836;&#37096;&#12289;&#33050;&#37096;&#25110;&#36328;&#22810;&#20010;&#39029;&#38754;&#20849;&#20139;&#30340;&#20869;&#23481;&#38750;&#24120;&#26377;&#29992;&#12290;<\/p>\n<pre><code>&lt;!-- header.php --&gt;\n&lt;header&gt;\n  &lt;h1&gt;Welcome to My Website&lt;\/h1&gt;\n&lt;\/header&gt;\n&lt;!-- end of header.php --&gt;\n\n&lt;!-- index.php --&gt;\n&lt;?php\ninclude 'header.php';\n?&gt;\n&lt;main&gt;\n  &lt;!-- Main content of your website --&gt;\n&lt;\/main&gt;\n<\/code><\/pre>\n<h2>include_once()<\/h2>\n<p>include_once() &#20989;&#25968;&#19982; include() &#31867;&#20284;&#65292;&#20294;&#23427;&#21482;&#20250;&#21253;&#21547;&#25991;&#20214;&#19968;&#27425;&#12290;&#22914;&#26524;&#23581;&#35797;&#20877;&#27425;&#21253;&#25324;&#21516;&#19968;&#20010;&#25991;&#20214;&#65292;&#23427;&#19981;&#20250;&#34987;&#21253;&#21547;&#65292;&#24182;&#19988;&#19981;&#20250;&#24341;&#36215;&#38169;&#35823;&#12290;&#36825;&#22312;&#21253;&#25324;&#30340;&#25991;&#20214;&#21253;&#21547;&#20989;&#25968;&#25110;&#31867;&#26102;&#38750;&#24120;&#26377;&#29992;&#65292;&#22240;&#20026;&#20877;&#27425;&#21253;&#25324;&#21487;&#33021;&#20250;&#23548;&#33268;&#33268;&#21629;&#38169;&#35823;&#12290;<\/p>\n<pre><code>&lt;!-- functions.php --&gt;\n&lt;?php\nfunction foo() {\n  echo 'Hello, I am a function.';\n}\n?&gt;\n\n&lt;!-- another-page.php --&gt;\n&lt;?php\ninclude_once 'functions.php';\nfoo(); \/\/ Output: Hello, I am a function.\ninclude_once 'functions.php'; \/\/ This will not include the file again.\n?&gt;\n<\/code><\/pre>\n<h2>&#29702;&#35299;require()&#26041;&#27861;&#12290;<\/h2>\n<p>require() &#20989;&#25968;&#19982; include() &#31867;&#20284;&#65292;&#20294;&#24403;&#22833;&#36133;&#26102;&#65292;&#23427;&#20250;&#24341;&#21457;&#33268;&#21629;&#38169;&#35823;&#24182;&#20572;&#27490;&#33050;&#26412;&#36816;&#34892;&#12290;&#22312;&#24212;&#29992;&#20013;&#20351;&#29992; require &#26102;&#65292;&#35831;&#21153;&#24517;&#30830;&#20445;&#25991;&#20214;&#23545;&#24212;&#29992;&#31243;&#24207;&#33267;&#20851;&#37325;&#35201;&#12290;<\/p>\n<pre><code>&lt;!-- config.php --&gt;\n&lt;?php\n\/\/ Database configuration\n$host = 'localhost';\n$username = 'root';\n$password = 'root';\n?&gt;\n\n&lt;!-- index.php --&gt;\n&lt;?php\nrequire 'config.php';\n\/\/ If config.php is not found, a fatal error will be thrown.\n?&gt;\n<\/code><\/pre>\n<h2>require_once()<\/h2>\n<p>&#19982;include_once&#31867;&#20284;&#65292;require_once&#20250;&#26816;&#26597;&#25991;&#20214;&#26159;&#21542;&#24050;&#32463;&#21253;&#21547;&#36807;&#19968;&#27425;&#65292;&#22914;&#26524;&#24050;&#21253;&#21547;&#65292;&#21017;&#19981;&#20877;&#37325;&#22797;&#21253;&#21547;&#12290;&#22914;&#26524;&#25991;&#20214;&#26080;&#27861;&#34987;&#21253;&#21547;&#65292;&#21017;&#20250;&#24341;&#21457;&#33268;&#21629;&#38169;&#35823;&#65292;&#24182;&#19988;&#26368;&#22909;&#29992;&#20110;&#25805;&#20316;&#33050;&#26412;&#24517;&#38656;&#30340;&#25991;&#20214;&#12290;<\/p>\n<pre><code>&lt;!-- init.php --&gt;\n&lt;?php\nrequire_once 'config.php';\nrequire_once 'functions.php';\n\/\/ Use this file to include essential components ...\n?&gt;\n<\/code><\/pre>\n<h2>&#39640;&#32423;&#21253;&#23481;&#25216;&#26415;<\/h2>\n<p>&#23545;&#20110;&#38656;&#35201;&#26356;&#22810;&#25511;&#21046;&#30340;&#24320;&#21457;&#20154;&#21592;&#65292;PHP &#21253;&#21547;&#20102;&#22914; file_exists() &#21644; is_readable() &#31561;&#20989;&#25968;&#65292;&#21487;&#20197;&#29992;&#26469;&#22312;&#23581;&#35797;&#21253;&#21547;&#25991;&#20214;&#20043;&#21069;&#26816;&#26597;&#25991;&#20214;&#30340;&#23384;&#22312;&#24615;&#21644;&#26435;&#38480;&#12290;<\/p>\n<pre><code>&lt;!-- index.php --&gt;\n&lt;?php\n$file = 'config.php';\nif (file_exists($file) &amp;&amp; is_readable($file)) {\n    require $file;\n} else {\n    die('The required file is not available.');\n}\n?&gt;\n<\/code><\/pre>\n<h2>&#33258;&#21160;&#21152;&#36733;&#31867;<\/h2>\n<p>&#22312;&#38754;&#21521;&#23545;&#35937;&#30340;&#32534;&#31243;&#29615;&#22659;&#20013;&#65292;PHP &#25552;&#20379;&#20102;&#19968;&#20010; spl_autoload_register &#20989;&#25968;&#65292;&#21487;&#20197;&#33258;&#21160;&#21152;&#36733;&#31867;&#65292;&#21363;&#39318;&#27425;&#20351;&#29992;&#26576;&#20010;&#31867;&#26102;&#20250;&#21253;&#25324;&#35813;&#31867;&#23545;&#24212;&#30340;&#25991;&#20214;&#12290;<\/p>\n<pre><code>&lt;!-- autoload.php --&gt;\n&lt;?php\nspl_autoload_register(function ($class_name) {\n    include $class_name . '.php';\n});\n?&gt;\n\n&lt;!-- MyClass.php --&gt;\n&lt;!-- Assuming the class name is MyClass and it's defined in MyClass.php --&gt;\n\n&lt;!-- index.php --&gt;\n&lt;?php\ninclude 'autoload.php';\n$myClass = new MyClass(); \/\/ The MyClass.php file is automatically included.\n?&gt;\n<\/code><\/pre>\n<h2>&#22788;&#29702;&#36335;&#24452;<\/h2>\n<p>&#31649;&#29702;&#25991;&#20214;&#36335;&#24452;&#23545;&#20110;&#21253;&#25324;&#25991;&#20214;&#33267;&#20851;&#37325;&#35201;&#12290;&#20320;&#21487;&#20197;&#20351;&#29992; __DIR__ &#21644; dirname(__FILE__) &#30830;&#20445;&#27491;&#30830;&#21253;&#21547;&#25991;&#20214;&#30340;&#30446;&#24405;&#36335;&#24452;&#12290;<\/p>\n<pre><code>&lt;!-- subfolder\/index.php --&gt;\n&lt;?php\nrequire __DIR__.'\/..\/config.php'; \/\/ Includes the file from the parent directory.\n?&gt;\n<\/code><\/pre>\n<p>&#26126;&#26234;&#22320;&#20351;&#29992;&#36825;&#20123;&#26041;&#27861;&#65292;&#20320;&#21487;&#20197;&#26500;&#24314;&#20986;&#26082;&#21487;&#25193;&#23637;&#21448;&#32452;&#32455;&#33391;&#22909;&#30340;PHP&#24212;&#29992;&#31243;&#24207;&#65281;<\/p>\n<h2>&#25688;&#35201;<\/h2>\n<p>&#22312;&#26412;&#25945;&#31243;&#20013;&#65292;&#24744;&#24050;&#23398;&#20064;&#20102;&#22914;&#20309;&#36890;&#36807;&#22312;&#38656;&#35201;&#26102;&#21253;&#21547;&#21478;&#19968;&#20010;PHP&#25991;&#20214;&#30340;&#26041;&#27861;&#26469;&#21253;&#25324;&#19968;&#20010;PHP&#25991;&#20214;&#12290;&#36890;&#36807;&#23558;&#20195;&#30721;&#32452;&#32455;&#21040;&#21333;&#29420;&#30340;&#25991;&#20214;&#20013;&#24182;&#26681;&#25454;&#38656;&#35201;&#21253;&#21547;&#36825;&#20123;&#25991;&#20214;&#65292;&#24744;&#21487;&#20197;&#20351;&#20195;&#30721;&#26356;&#21152;&#21487;&#35835;&#12289;&#32500;&#25252;&#21644;&#21487;&#25193;&#23637;&#12290;&#26080;&#35770;&#24744;&#20351;&#29992;include()&#12289;require()&#12289;&#23427;&#20204;&#30340;&ldquo;&#19968;&#27425;&rdquo;&#21464;&#20307;&#36824;&#26159;&#33258;&#21160;&#29983;&#25104;&#22120;&#65292;&#35760;&#20303;&#36873;&#25321;&#36866;&#21512;&#24212;&#29992;&#31243;&#24207;&#38656;&#27714;&#30340;&#26041;&#27861;&#26159;&#32534;&#20889;&#25972;&#27905;&#39640;&#25928;&#20195;&#30721;&#30340;&#20851;&#38190;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#20171;&#32461; &#22312;PHP&#20013;&#65292;&#20174;&#21478;&#19968;&#20010;&#25991;&#20214;&#21253;&#21547;&#20195;&#30721;&#26159;&#19968;&#31181;&#24120;&#038;#35..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-428","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\/428","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=428"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=428"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=428"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}