⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 function.include.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!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&#039;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 &quot;b.php&quot;</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">&lt;?php<br /><br />$color&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'green'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$fruit&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;<br /></span><br />test.php<br /><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"A&nbsp;$color&nbsp;$fruit"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;A<br /><br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'vars.php'</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"A&nbsp;$color&nbsp;$fruit"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;A&nbsp;green&nbsp;apple<br /><br /></span><span style="color: #0000BB">?&gt;</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">&lt;?php<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;global&nbsp;</span><span style="color: #0000BB">$color</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;include&nbsp;</span><span style="color: #DD0000">'vars.php'</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"A&nbsp;$color&nbsp;$fruit"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;vars.php&nbsp;is&nbsp;in&nbsp;the&nbsp;scope&nbsp;of&nbsp;foo()&nbsp;so&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*<br />&nbsp;*&nbsp;$fruit&nbsp;is&nbsp;NOT&nbsp;available&nbsp;outside&nbsp;of&nbsp;this&nbsp;&nbsp;*<br />&nbsp;*&nbsp;scope.&nbsp;&nbsp;$color&nbsp;is&nbsp;because&nbsp;we&nbsp;declared&nbsp;it&nbsp;*<br />&nbsp;*&nbsp;as&nbsp;global.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/<br /><br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;A&nbsp;green&nbsp;apple<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"A&nbsp;$color&nbsp;$fruit"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;A&nbsp;green<br /><br /></span><span style="color: #0000BB">?&gt;</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 &quot;<a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">URL fopen wrappers</a>&quot;     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&#039;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">&lt;?php<br /><br /></span><span style="color: #FF8000">/*&nbsp;This&nbsp;example&nbsp;assumes&nbsp;that&nbsp;www.example.com&nbsp;is&nbsp;configured&nbsp;to&nbsp;parse&nbsp;.php<br />&nbsp;*&nbsp;files&nbsp;and&nbsp;not&nbsp;.txt&nbsp;files.&nbsp;Also,&nbsp;'Works'&nbsp;here&nbsp;means&nbsp;that&nbsp;the&nbsp;variables<br />&nbsp;*&nbsp;$foo&nbsp;and&nbsp;$bar&nbsp;are&nbsp;available&nbsp;within&nbsp;the&nbsp;included&nbsp;file.&nbsp;*/<br /><br />//&nbsp;Won't&nbsp;work;&nbsp;file.txt&nbsp;wasn't&nbsp;handled&nbsp;by&nbsp;www.example.com&nbsp;as&nbsp;PHP<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'http://www.example.com/file.txt?foo=1&amp;bar=2'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Won't&nbsp;work;&nbsp;looks&nbsp;for&nbsp;a&nbsp;file&nbsp;named&nbsp;'file.php?foo=1&amp;bar=2'&nbsp;on&nbsp;the<br />//&nbsp;local&nbsp;filesystem.<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'file.php?foo=1&amp;bar=2'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Works.<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'http://www.example.com/file.php?foo=1&amp;bar=2'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />include&nbsp;</span><span style="color: #DD0000">'file.txt'</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Works.<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'file.php'</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Works.<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>     </div>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -