【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最新問題將 jpg 圖片從 mysql 新增到 html 作為序言,我對此很陌生,所以這可能是一個愚蠢的問題,但是......我正在嘗試設計一個從數據庫中提取的網站,我在該數據庫中存儲了“商店”的產品信息。這是我到目前為止的程式碼。 se...來自於 2024-04-02 13:27:3401344