📄 function.include.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>include</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.require.html">require</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.require-once.html">require_once</a></div> <div class="up"><a href="language.control-structures.html">Control Structures</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.include" class="sect1"> <h2 class="title"><b>include()</b></h2> <p class="simpara"> The <b>include()</b> statement includes and evaluates the specified file. </p> <p class="simpara"> The documentation below also applies to <b>require()</b>. The two constructs are identical in every way except how they handle failure. They both produce a <a href="errorfunc.constants.html#errorfunc.constants.errorlevels.e-warning" class="link">Warning</a>, but <b>require()</b> results in a <a href="errorfunc.constants.html#errorfunc.constants.errorlevels.e-error" class="link">Fatal Error</a>. In other words, use <b>require()</b> if you want a missing file to halt processing of the page. <b>include()</b> does not behave this way, the script will continue regardless. Be sure to have an appropriate <a href="ini.core.html#ini.include-path" class="link">include_path</a> setting as well. Be warned that parse error in included file doesn't cause processing halting in PHP versions prior to PHP 4.3.5. Since this version, it does. </p> <p class="simpara"> Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script. E.g. if your include_path is <i>libraries</i>, current working directory is <var class="filename">/www/</var>, you included <var class="filename">include/a.php</var> and there is <i>include "b.php"</i> in that file, <var class="filename">b.php</var> is first looked in <var class="filename">/www/libraries/</var> and then in <var class="filename">/www/include/</var>. If filename begins with <i>./</i> or <i>../</i>, it is looked only in the current working directory. </p> <p class="simpara"> When a file is included, the code it contains inherits the <a href="language.variables.scope.html" class="link">variable scope</a> of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope. </p> <p class="para"> <div class="example"> <p><b>Example #1 Basic <b>include()</b> example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000">vars.php<br /><span style="color: #0000BB"><?php<br /><br />$color </span><span style="color: #007700">= </span><span style="color: #DD0000">'green'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$fruit </span><span style="color: #007700">= </span><span style="color: #DD0000">'apple'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?><br /></span><br />test.php<br /><span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"A $color $fruit"</span><span style="color: #007700">; </span><span style="color: #FF8000">// A<br /><br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'vars.php'</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"A $color $fruit"</span><span style="color: #007700">; </span><span style="color: #FF8000">// A green apple<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="simpara"> If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function. An exception to this rule are <a href="language.constants.predefined.html" class="link">magic constants</a> which are evaluated by the parser before the include occurs. </p> <p class="para"> <div class="example"> <p><b>Example #2 Including within functions</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">()<br />{<br /> global </span><span style="color: #0000BB">$color</span><span style="color: #007700">;<br /><br /> include </span><span style="color: #DD0000">'vars.php'</span><span style="color: #007700">;<br /><br /> echo </span><span style="color: #DD0000">"A $color $fruit"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">/* vars.php is in the scope of foo() so *<br /> * $fruit is NOT available outside of this *<br /> * scope. $color is because we declared it *<br /> * as global. */<br /><br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">(); </span><span style="color: #FF8000">// A green apple<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"A $color $fruit"</span><span style="color: #007700">; </span><span style="color: #FF8000">// A green<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="simpara"> When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within <a href="language.basic-syntax.html#language.basic-syntax.phpmode" class="link">valid PHP start and end tags</a>. </p> <p class="simpara"> If "<a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">URL fopen wrappers</a>" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper - see <a href="wrappers.html" class="xref">List of Supported Protocols/Wrappers</a> for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script. </p> <div class="warning"><b class="warning">Warning</b><p class="para">Windows versions of PHPprior to PHP 4.3.0 do not support access of remote files via this function,even if <a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">allow_url_fopen</a> is enabled.</p></div> <p class="para"> <div class="example"> <p><b>Example #3 <b>include()</b> through HTTP</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">/* This example assumes that www.example.com is configured to parse .php<br /> * files and not .txt files. Also, 'Works' here means that the variables<br /> * $foo and $bar are available within the included file. */<br /><br />// Won't work; file.txt wasn't handled by www.example.com as PHP<br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'http://www.example.com/file.txt?foo=1&bar=2'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the<br />// local filesystem.<br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'file.php?foo=1&bar=2'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Works.<br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'http://www.example.com/file.php?foo=1&bar=2'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />include </span><span style="color: #DD0000">'file.txt'</span><span style="color: #007700">; </span><span style="color: #FF8000">// Works.<br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'file.php'</span><span style="color: #007700">; </span><span style="color: #FF8000">// Works.<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -