【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的区别본 웹사이트의 성명본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.저자별 최신 기사
2024-10-22 09:46:29 2024-10-13 13:53:41 2024-10-12 12:15:51 2024-10-11 22:47:31 2024-10-11 19:36:51 2024-10-11 15:50:41 2024-10-11 15:07:41 2024-10-11 14:21:21 2024-10-11 12:59:11 2024-10-11 12:17:31최신 이슈mysql의 jpg 이미지를 html에 추가 서문으로서 저는 이것에 대해 매우 생소하기 때문에 어리석은 질문일 수도 있습니다. 그러나... 저는 "상점"에 대한 제품 정보를 저장하는 데이터...에서 2024-04-02 13:27:3401344