【PHP】区分include(),require(),include_once(),require_once()
作者:zhanhailiang 日期:2013-03-19
include(),require()语句包含并运行指定文件。 这两种结构除了在如何处理失败之外完全一样。
include()产生一个警告而require()则导致一个致命错误。换句话说,如果想在遇到丢失文件时停止处理页面就用 require()。include()就不是这样,脚本会继续运行例1:include()产生一个Warning而require()则导致一个Fatal error。
<span style="color:rgb(51,51,51)">zhanhailiang<span class="sy0" style="color:rgb(102,204,102)">@</span>linux-06bq:~<span class="sy0" style="color:rgb(102,204,102)">></span> php <span class="re5">-r</span> <span class="st0" style="color:rgb(255,0,0)">"include('a.php');"</span> Warning: include<span class="br0" style="color:rgb(102,204,102)">(</span>a.php<span class="br0" style="color:rgb(102,204,102)">)</span>: failed to open stream: No such <span class="kw2" style="color:rgb(0,0,0); font-weight:bold">file</span> or directory <span class="kw1" style="color:rgb(177,177,0)">in</span> Command line code on line <span class="nu0" style="color:rgb(204,102,204)">1</span> </span><span style="color:#ff0000"><strong>Warning</strong></span><span style="color:#333333">: include</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333">: Failed opening </span><span class="st_h" style="color:rgb(51,51,51)">'a.php'</span><span style="color:#333333"> </span><span class="kw1" style="color:rgb(177,177,0)">for</span><span style="color:#333333"> inclusion </span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="re2" style="color:rgb(0,0,255)">include_path</span><span style="color:#333333">=</span><span class="st_h" style="color:rgb(51,51,51)">'.:/usr/local/services/phplib/src:/usr/local/services/phplib/inc:/usr/local/services/php/lib/php'</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333"> </span><span class="kw1" style="color:rgb(177,177,0)">in</span><span style="color:#333333"> Command line code on line </span><span class="nu0" style="color:rgb(204,102,204)">1</span><span style="color:#333333"> zhanhailiang</span><span class="sy0" style="color:rgb(102,204,102)">@</span><span style="color:#333333">linux-06bq:~</span><span class="sy0" style="color:rgb(102,204,102)">></span><span style="color:#333333"> php </span><span class="re5" style="color:rgb(51,51,51)">-r</span><span style="color:#333333"> </span><span class="st0" style="color:rgb(255,0,0)">"require('a.php');"</span><span style="color:#333333"> Warning: require</span><span class="br0" style="color:rgb(102,204,102)">(</span><span style="color:#333333">a.php</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333">: failed to open stream: No such </span><span class="kw2" style="color:rgb(0,0,0); font-weight:bold">file</span><span style="color:#333333"> or directory </span><span class="kw1" style="color:rgb(177,177,0)">in</span><span style="color:#333333"> Command line code on line </span><span class="nu0" style="color:rgb(204,102,204)">1</span><span style="color:#333333"> </span><strong><span style="color:#ff0000">Fatal error</span></strong><span style="color:#333333">: require</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333">: Failed opening required </span><span class="st_h" style="color:rgb(51,51,51)">'a.php'</span><span style="color:#333333"> </span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="re2" style="color:rgb(0,0,255)">include_path</span><span style="color:#333333">=</span><span class="st_h" style="color:rgb(51,51,51)">'.:/usr/local/services/phplib/src:/usr/local/services/phplib/inc:/usr/local/services/php/lib/php'</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333"> </span><span class="kw1" style="color:rgb(177,177,0)">in</span><span style="color:#333333"> Command line code on line </span><span class="nu0" style="color:rgb(204,102,204)">1</span>2.include()与include_once()的区别(同理可区别require()与require_once())
include_once()语句在脚本执行期间包含并运行指定文件。此行为和include()语句类似,唯一区别是如果该文件中的代码已经被包含了,则不会再次包含。如同此语句名字暗示的那样,只会包含一次。
include_once()应该用于在脚本执行期间同一个文件有可能被包含超过一次的情况下,想确保它只被包含一次以避免函数重定义,变量重新赋值等问题。
返回值和include()相同。如果文件已被包含,本函数返回TRUE。例1:include()会多次包含指定文件,而include_once()不会。
zhanhailiang<span class="sy0" style="color:rgb(102,204,102)">@</span>linux-06bq:~<span class="sy0" style="color:rgb(102,204,102)">></span> <span class="kw2" style="color:rgb(0,0,0); font-weight:bold">cat</span> a.php <span class="sy0" style="color:rgb(102,204,102)"><</span>?php <span class="kw3" style="color:rgb(0,0,102)">echo</span> <span class="st_h">'1'</span>.PHP_EOL; zhanhailiang<span class="sy0" style="color:rgb(102,204,102)">@</span>linux-06bq:~<span class="sy0" style="color:rgb(102,204,102)">></span> php <span class="re5">-r</span> <span class="st0" style="color:rgb(255,0,0)">"include('a.php');include('a.php');"</span> <span class="nu0" style="color:rgb(204,102,204)">1</span> <span class="nu0" style="color:rgb(204,102,204)">1</span> zhanhailiang<span class="sy0" style="color:rgb(102,204,102)">@</span>linux-06bq:~<span class="sy0" style="color:rgb(102,204,102)">></span> php <span class="re5">-r</span> <span class="st0" style="color:rgb(255,0,0)">"include_once('a.php');include_once('a.php');"</span> <span class="nu0" style="color:rgb(204,102,204)">1</span>例2:include_once()避免函数重定义。
<span style="color:#333333">zhanhailiang</span><span class="sy0" style="color:rgb(102,204,102)">@</span><span style="color:#333333">linux-06bq:~</span><span class="sy0" style="color:rgb(102,204,102)">></span><span style="color:#333333"> </span><span class="kw2" style="color:rgb(0,0,0); font-weight:bold">cat</span><span style="color:#333333"> a.php </span><span class="sy0" style="color:rgb(102,204,102)"><</span><span style="color:#333333">?php </span><span class="kw3" style="color:rgb(0,0,102)">echo</span><span style="color:#333333"> </span><span class="st_h" style="color:rgb(51,51,51)">'1'</span><span style="color:#333333">.PHP_EOL; </span><span class="kw1" style="color:rgb(177,177,0)">function</span><span style="color:#333333"> </span><span class="kw3" style="color:rgb(0,0,102)">test</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333"> </span><span class="br0" style="color:rgb(102,204,102)">{</span><span class="br0" style="color:rgb(102,204,102)">}</span><span style="color:#333333"> zhanhailiang</span><span class="sy0" style="color:rgb(102,204,102)">@</span><span style="color:#333333">linux-06bq:~</span><span class="sy0" style="color:rgb(102,204,102)">></span><span style="color:#333333"> php </span><span class="re5" style="color:rgb(51,51,51)">-r</span><span style="color:#333333"> </span><span class="st0" style="color:rgb(255,0,0)">"include('a.php');include('a.php');"</span><span style="color:#333333"> </span><span class="nu0" style="color:rgb(204,102,204)">1</span><span style="color:#333333"> </span><strong><span style="color:#ff0000">Fatal error</span></strong><span style="color:#333333">: Cannot redeclare </span><span class="kw3" style="color:rgb(0,0,102)">test</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333"> </span><span class="br0" style="color:rgb(102,204,102)">(</span><span style="color:#333333">previously declared </span><span class="kw1" style="color:rgb(177,177,0)">in</span><span style="color:#333333"> </span><span class="sy0" style="color:rgb(102,204,102)">/</span><span style="color:#333333">home</span><span class="sy0" style="color:rgb(102,204,102)">/</span><span style="color:#333333">zhanhailiang</span><span class="sy0" style="color:rgb(102,204,102)">/</span><span style="color:#333333">a.php:</span><span class="nu0" style="color:rgb(204,102,204)">4</span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333"> </span><span class="kw1" style="color:rgb(177,177,0)">in</span><span style="color:#333333"> </span><span class="sy0" style="color:rgb(102,204,102)">/</span><span style="color:#333333">home</span><span class="sy0" style="color:rgb(102,204,102)">/</span><span style="color:#333333">zhanhailiang</span><span class="sy0" style="color:rgb(102,204,102)">/</span><span style="color:#333333">a.php on line </span><span class="nu0" style="color:rgb(204,102,204)">4</span><span style="color:#333333"> zhanhailiang</span><span class="sy0" style="color:rgb(102,204,102)">@</span><span style="color:#333333">linux-06bq:~</span><span class="sy0" style="color:rgb(102,204,102)">></span><span style="color:#333333"> php </span><span class="re5" style="color:rgb(51,51,51)">-r</span><span style="color:#333333"> </span><span class="st0" style="color:rgb(255,0,0)">"include_once('a.php');include_once('a.php');"</span><span style="color:#333333"> </span><span class="nu0" style="color:rgb(204,102,204)">1</span>3.扩展阅读
include require include_once require_once 从php核心代码看require和include的区别