require 和 include 几乎完全一样,除了处理失败的方式不同之外。require 在出错时产 ...
<div id="article_content" class="article_content clearfix"><link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
<link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-6e43165c0a.css">
<div id="content_views" class="htmledit_views">
<p>(PHP 4, PHP 5, PHP 7, PHP 8)</p>
<p><code>require</code> 和 <ahref="https://www.php.net/manual/zh/function.include.php">include</a> 几乎完全一样,除了处理失败的方式不同之外。<strong>require</strong> 在出错时产生 <strong><code>E_COMPILE_ERROR</code></strong> 级别的错误。换句话说将导致脚本中止而 <ahref="https://www.php.net/manual/zh/function.include.php">include</a> 只产生警告(<strong><code>E_WARNING</code></strong>),脚本会继续运行。</p>
<p>参见 <ahref="https://www.php.net/manual/zh/function.include.php">include</a> 文档了解详情。</p>
<p><ahref="https://www.php.net/manual/add-note.php?sect=function.require&redirect=https://www.php.net/manual/zh/function.require.php">正在上传…重新上传取消 add a note</a></p>
<h3>User Contributed Notes 26 notes</h3>
<p><ahref="https://www.php.net/manual/vote-note.php?id=75875&page=function.require&vote=up">up</a></p>
<p><ahref="https://www.php.net/manual/vote-note.php?id=75875&page=function.require&vote=down">down</a></p>
<p>130</p>
<p><ahref="https://www.php.net/manual/zh/function.require.php#75875">chris at chrisstockton dot org</a><ahref="https://www.php.net/manual/zh/function.require.php#75875"> ¶</a></p>
<p><strong>14 years ago</strong></p>
<p><code>Remember, when using require that it is a statement, not a function. It's not necessary to write:<br> <?php<br> require('somefile.php');<br> ?><br><br> The following:<br> <?php<br> require 'somefile.php';<br> ?><br><br> Is preferred, it will prevent your peers from giving you a hard time and a trivial conversation about what require really is.</code></p>
<p><ahref="https://www.php.net/manual/vote-note.php?id=126279&page=function.require&vote=up">up</a></p>
<p><ahref="https://www.php.net/manual/vote-note.php?id=126279&page=function.require&vote=down">down</a></p>
<p>10</p>
<p><ahref="https://www.php.net/manual/zh/function.require.php#126279">Marcel Baur</a><ahref="https://www.php.net/manual/zh/function.require.php#126279"> ¶</a></p>
<p><strong>6 months ago</strong></p>
<p><code>If your included file returns a value, you can get it as a result from require(), i.e.<br><br> foo.php:<br> <?php<br> return "foo";<br> ?><br><br> $bar = require("foo.php");<br> echo $bar; // equals to "foo"</code></p>
<p><ahref="https://www.php.net/manual/vote-note.php?id=72742&page=function.require&vote=up">up</a></p>
<p><ahref="https://www.php.net/manual/vote-note.php?id=72742&page=function.require&vote=down">down</a></p>
<p>0</p>
<p><ahref="https://www.php.net/manual/zh/function.require.php#72742">Anonymous</a><ahref="https://www.php.net/manual/zh/function.require.php#72742"> ¶</a></p>
<p><strong>15 years ago</strong></p>
<p><code>A note that drove me nuts for 2 days!<br><br> Be carfull if you have a newline or blank space befor your php tags in the included/required file it will read as html and outputed.<br><br> If your running your output through javascript string evaluations which would be sensitive to newlines/white spaces be carfull that the first chars in the file are the php tages eg <?php</code></p>
<p><ahref="https://www.php.net/manual/vote-note.php?id=117670&page=function.require&vote=up">up</a></p>
<p><ahref="https://www.php.net/manual/vote-note.php?id=117670&page=function.require&vote=down">down</a></p>
<p>-9</p>
<p><ahref="https://www.php.net/manual/zh/function.require.php#117670">dank at kegel dot com</a><ahref="https://www.php.net/manual/zh/function.require.php#117670"> ¶</a></p>
<p><strong>6 years ago</strong></p>
<p><code>PHP's require and include seem to differ from C's include in another way: they can't be used in the middle of an expression. e.g.<br><br> $ more foo1.php foo2.php<br> ::::::::::::::<br> foo1.php<br> ::::::::::::::<br> <?php<br> print "hello"<br> .<br> #"there"<br> require 'foo2.php';<br> . "\n";<br> ?><br> ::::::::::::::<br> foo2.php<br> ::::::::::::::<br> "there"<br> $ php foo1.php<br> PHP Parse error: syntax error, unexpected '.' in foo1.php on line 6<br><br> So php's include operates only on complete statements, whereas c's include operates on bytes of source code.</code></p>
<p><ahref="https://www.php.net/manual/vote-note.php?id=51699&page=function.require&vote=up">up</a></p>
<p><ahref="https://www.php.net/manual/vote-note.php?id=51699&page=function.require&vote=down">down</a></p>
<p>-8</p>
<p><ahref="https://www.php.net/manual/zh/function.require.php#51699">richardbrenner(-at- )gmx(-)at</a><ahref="https://www.php.net/manual/zh/function.require.php#51699"> ¶</a></p>
<p><strong>16 years ago</strong></p>
<p><code>If you use relativ paths in a php script (file A) that can be required by another php script (file B), be aware that the relativ paths in file A will be relativ to the directory, where file B is stored.<br> You can use the following syntax in file A, to be sure that the paths are relativ to the directory of file A:<br><br> <?<br> require(dirname(__FILE__)."/path/relative/file_to_include.php");<br> ?><br><br> Greetings,<br> Richard</code></p>
</div>
</div>
<div id="treeSkill"></div>
页:
[1]