admin 发表于 2023-2-16 18:54:50

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> 几乎完全一样&#xff0c;除了处理失败的方式不同之外。<strong>require</strong> 在出错时产生 <strong><code>E_COMPILE_ERROR</code></strong> 级别的错误。换句话说将导致脚本中止而 <ahref="https://www.php.net/manual/zh/function.include.php">include</a> 只产生警告&#xff08;<strong><code>E_WARNING</code></strong>&#xff09;&#xff0c;脚本会继续运行。</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&#61;function.require&redirect&#61;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&#61;75875&page&#61;function.require&vote&#61;up">up</a></p>
<p><ahref="https://www.php.net/manual/vote-note.php?id&#61;75875&page&#61;function.require&vote&#61;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&#61;126279&page&#61;function.require&vote&#61;up">up</a></p>
<p><ahref="https://www.php.net/manual/vote-note.php?id&#61;126279&page&#61;function.require&vote&#61;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 &#34;foo&#34;;<br> ?><br><br> $bar &#61; require(&#34;foo.php&#34;);<br> echo $bar; // equals to &#34;foo&#34;</code></p>
<p><ahref="https://www.php.net/manual/vote-note.php?id&#61;72742&page&#61;function.require&vote&#61;up">up</a></p>
<p><ahref="https://www.php.net/manual/vote-note.php?id&#61;72742&page&#61;function.require&vote&#61;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&#61;117670&page&#61;function.require&vote&#61;up">up</a></p>
<p><ahref="https://www.php.net/manual/vote-note.php?id&#61;117670&page&#61;function.require&vote&#61;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 &#34;hello&#34;<br> .<br> #&#34;there&#34;<br> require 'foo2.php';<br> . &#34;\n&#34;;<br> ?><br> ::::::::::::::<br> foo2.php<br> ::::::::::::::<br> &#34;there&#34;<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&#61;51699&page&#61;function.require&vote&#61;up">up</a></p>
<p><ahref="https://www.php.net/manual/vote-note.php?id&#61;51699&page&#61;function.require&vote&#61;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__).&#34;/path/relative/file_to_include.php&#34;);<br> ?><br><br> Greetings,<br> Richard</code></p>
                </div>
      </div>
      <div id="treeSkill"></div>
页: [1]
查看完整版本: require 和 include 几乎完全一样,除了处理失败的方式不同之外。require 在出错时产 ...