📄 function.include.html
字号:
</div> </p> <div class="warning"><b class="warning">Warning</b> <h1 class="title">Security warning</h1> <p class="para"> Remote file may be processed at the remote server (depending on the file extension and the fact if the remote server runs PHP or not) but it still has to produce a valid PHP script because it will be processed at the local server. If the file from the remote server should be processed there and outputted only, <a href="function.readfile.html" class="function">readfile()</a> is much better function to use. Otherwise, special care should be taken to secure the remote script to produce a valid and desired code. </p> </div> <p class="para"> See also <a href="features.remote-files.html" class="link">Remote files</a>, <a href="function.fopen.html" class="function">fopen()</a> and <a href="function.file.html" class="function">file()</a> for related information. </p> <p class="simpara"> Handling Returns: It is possible to execute a <b>return()</b> statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it's possible to return values from included files. You can take the value of the include call as you would a normal function. This is not, however, possible when including remote files unless the output of the remote file has <a href="language.basic-syntax.html#language.basic-syntax.phpmode" class="link">valid PHP start and end tags</a> (as with any local file). You can declare the needed variables within those tags and they will be introduced at whichever point the file was included. </p> <p class="para"> Because <b>include()</b> is a special language construct, parentheses are not needed around its argument. Take care when comparing return value. <div class="example"> <p><b>Example #4 Comparing return value of include</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// won't work, evaluated as include(('vars.php') == 'OK'), i.e. include('')<br /></span><span style="color: #007700">if (include(</span><span style="color: #DD0000">'vars.php'</span><span style="color: #007700">) == </span><span style="color: #DD0000">'OK'</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">'OK'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// works<br /></span><span style="color: #007700">if ((include </span><span style="color: #DD0000">'vars.php'</span><span style="color: #007700">) == </span><span style="color: #DD0000">'OK'</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">'OK'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #5 <b>include()</b> and the <b>return()</b> statement</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000">return.php<br /><span style="color: #0000BB"><?php<br /><br />$var </span><span style="color: #007700">= </span><span style="color: #DD0000">'PHP'</span><span style="color: #007700">;<br /><br />return </span><span style="color: #0000BB">$var</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?><br /></span><br />noreturn.php<br /><span style="color: #0000BB"><?php<br /><br />$var </span><span style="color: #007700">= </span><span style="color: #DD0000">'PHP'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?><br /></span><br />testreturns.php<br /><span style="color: #0000BB"><?php<br /><br />$foo </span><span style="color: #007700">= include </span><span style="color: #DD0000">'return.php'</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">$foo</span><span style="color: #007700">; </span><span style="color: #FF8000">// prints 'PHP'<br /><br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= include </span><span style="color: #DD0000">'noreturn.php'</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">$bar</span><span style="color: #007700">; </span><span style="color: #FF8000">// prints 1<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="simpara"> <i>$bar</i> is the value <i>1</i> because the include was successful. Notice the difference between the above examples. The first uses <b>return()</b> within the included file while the other does not. If the file can't be included, <b><tt>FALSE</tt></b> is returned and <i>E_WARNING</i> is issued. </p> <p class="para"> If there are functions defined in the included file, they can be used in the main file independent if they are before <b>return()</b> or after. If the file is included twice, PHP 5 issues fatal error because functions were already declared, while PHP 4 doesn't complain about functions defined after <b>return()</b>. It is recommended to use <b>include_once()</b> instead of checking if the file was already included and conditionally return inside the included file. </p> <p class="simpara"> Another way to "include" a PHP file into a variable is to capture the output by using the <a href="ref.outcontrol.html" class="link">Output Control Functions</a> with <b>include()</b>. For example: </p> <p class="para"> <div class="example"> <p><b>Example #6 Using output buffering to include a PHP file into a string</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$string </span><span style="color: #007700">= </span><span style="color: #0000BB">get_include_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'somefile.php'</span><span style="color: #007700">);<br /><br />function </span><span style="color: #0000BB">get_include_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">) {<br /> if (</span><span style="color: #0000BB">is_file</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">ob_start</span><span style="color: #007700">();<br /> include </span><span style="color: #0000BB">$filename</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$contents </span><span style="color: #007700">= </span><span style="color: #0000BB">ob_get_contents</span><span style="color: #007700">();<br /> </span><span style="color: #0000BB">ob_end_clean</span><span style="color: #007700">();<br /> return </span><span style="color: #0000BB">$contents</span><span style="color: #007700">;<br /> }<br /> return </span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> In order to automatically include files within scripts, see also the <a href="ini.core.html#ini.auto-prepend-file" class="link">auto_prepend_file</a> and <a href="ini.core.html#ini.auto-append-file" class="link">auto_append_file</a> configuration options in <var class="filename">php.ini</var>. </p> <blockquote><p><b class="note">Note</b>: <span class="simpara">Because this is a language construct and not a function, it cannot be called using <a href="functions.variable-functions.html" class="link">variable functions</a></span></p></blockquote> <p class="simpara"> See also <b>require()</b>, <b>require_once()</b>, <b>include_once()</b>, <a href="function.get-included-files.html" class="function">get_included_files()</a>, <a href="function.readfile.html" class="function">readfile()</a>, <a href="function.virtual.html" class="function">virtual()</a>, and <a href="ini.core.html#ini.include-path" class="link">include_path</a>. </p> </div><hr /><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></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -