【PHP】界别include(),require(),include_once(),require_once()

WBOY
Freigeben: 2016-06-13 11:17:46
Original
910 Leute haben es durchsucht

【PHP】区分include(),require(),include_once(),require_once()

  作者:zhanhailiang 日期:2013-03-19
Nach dem Login kopieren

1.include()与require()的区别(同理可区分include_once()与require_once())

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

例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>
Nach dem Login kopieren

3.扩展阅读

  • include
  • require
  • include_once
  • require_once
  • 从php核心代码看require和include的区别
Verwandte Etiketten:
Quelle:php.cn
Vorheriger Artikel:sessionid 是什么时候发作的 Nächster Artikel:关于PHP下传的有关问题
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Artikel des Autors
Aktuelle Ausgaben
verwandte Themen
Mehr>
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!