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

return将程序控制返还给调用模块

<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">
                  <div id="function.return">
<p style="margin-left:0;"><span style="color:#333333;">&#xff08;PHP 4、PHP 5、PHP 7、PHP 8&#xff09;</span></p>
<p style="margin-left:0;"><span style="color:#333333;"><code>return</code>将程序控制返还给调用模块。 将在调用模块中执行的下一句表达式中继续。</span></p>
<p style="margin-left:0;"><span style="color:#333333;">如果在一个函数中调用 <strong>return</strong> 语句&#xff0c;将立即结束此函数的执行并将它的参数作为函数的值返回。<strong>return</strong> 也会终止 <ahref="https://www.php.net/manual/zh/function.eval.php">eval()</a> 语句或者脚本文件的执行。</span></p>
<p style="margin-left:0;"><span style="color:#333333;">如果在全局范围中调用&#xff0c;则当前脚本文件中止运行。如果当前脚本文件是被 <ahref="https://www.php.net/manual/zh/function.include.php">include</a> 的或者 <ahref="https://www.php.net/manual/zh/function.require.php">require</a> 的&#xff0c;则控制交回调用文件。此外&#xff0c;如果当前脚本是被 <ahref="https://www.php.net/manual/zh/function.include.php">include</a> 的&#xff0c;则 <strong>return</strong> 的值会被当作 <ahref="https://www.php.net/manual/zh/function.include.php">include</a> 调用的返回值。如果在主脚本文件中调用 <strong>return</strong>&#xff0c;则脚本中止运行。如果当前脚本文件是在 php.ini 中的配置选项 <ahref="https://www.php.net/manual/zh/ini.core.php#ini.auto-prepend-file">auto_prepend_file</a> 或者 <ahref="https://www.php.net/manual/zh/ini.core.php#ini.auto-append-file">auto_append_file</a> 所指定的&#xff0c;则此脚本文件中止运行。</span></p>
<p style="margin-left:0;"><span style="color:#333333;">更多信息见<ahref="https://www.php.net/manual/zh/functions.returning-values.php">返回值</a>。</span></p>
<p style="margin-left:0;"></p>
<blockquote>
<p style="margin-left:0;"><span style="color:#333333;"><strong>注意</strong>: 注意既然 <strong>return</strong> 是语言结构而不是函数&#xff0c;因此其参数没有必要用括号将其括起来&#xff0c;也不推荐这样用。</span></p>
</blockquote>
<blockquote>
<p style="margin-left:0;"><span style="color:#333333;"><strong>注意</strong>: 如果没有提供参数&#xff0c;则一定不能用括号&#xff0c;此时返回 <strong><code>null</code></strong>。如果调用 <code>return</code> 时加上了括号却又没有参数会导致解析错误。</span></p>
</blockquote>
<p style="margin-left:0;"><span style="color:#333333;">自 PHP 7.1.0 起&#xff0c;如果返回类型需要是 void 而带了返回的参数&#xff0c; 将导致 <strong><code>E_COMPILE_ERROR</code></strong>&#xff1b; 相反返回类型需要而未带参数也会同样导致该错误。</span></p>
</div>
<div>
<ahref="https://www.php.net/manual/add-note.php?sect&#61;function.return&redirect&#61;https://www.php.net/manual/zh/function.return.php">正在上传…重新上传取消 添加注释</a>
<h3 style="margin-left:0;"><span style="color:#793862;">用户贡献的笔记<span style="background-color:#793862;"><span style="color:#ffffff;">2 个音符</span></span></span></h3>
</div>
<div id="allnotes">
<div id="59866" style="margin-left:0;">
<div>
   <div id="Vu59866">
    <ahref="https://www.php.net/manual/vote-note.php?id&#61;59866&page&#61;function.return&vote&#61;up">向上</a>
   </div>
   <div id="Vd59866">
    <ahref="https://www.php.net/manual/vote-note.php?id&#61;59866&page&#61;function.return&vote&#61;down">向下</a>
   </div>
   <div id="V59866">
    <span style="color:#333333;">177</span>
   </div>
</div>
<ahref="https://www.php.net/manual/zh/function.return.php#59866">warhog 在 warhog dot net</a>
<ahref="https://www.php.net/manual/zh/function.return.php#59866"> ¶</a>
<div style="text-align:right;">
   <span style="color:#666666;"><strong>16年前</strong></span>
</div>
<div id="Hcom59866">
   <div style="text-align:left;">
    <span style="background-color:#ffffff;"><code><span style="color:#000000;">for those of you who think that using return in a script is the same as using exit note that: using return just exits the execution of the current script, exit the whole execution.<br><br> look at that example:<br><br> a.php<br><span style="color:#336699;"><?php</span><br><span style="color:#669933;">include(</span><span style="color:#cc3333;">&#34;b.php&#34;</span><span style="color:#669933;">);<br> echo </span><span style="color:#cc3333;">&#34;a&#34;</span><span style="color:#669933;">;</span><br><span style="color:#336699;">?></span><br><br> b.php<br><span style="color:#336699;"><?php</span><br><span style="color:#669933;">echo </span><span style="color:#cc3333;">&#34;b&#34;</span><span style="color:#669933;">;<br> return;</span><br><span style="color:#336699;">?></span><br><br> (executing a.php:) will echo &#34;ba&#34;.<br><br> whereas (b.php modified):<br><br> a.php<br><span style="color:#336699;"><?php</span><br><span style="color:#669933;">include(</span><span style="color:#cc3333;">&#34;b.php&#34;</span><span style="color:#669933;">);<br> echo </span><span style="color:#cc3333;">&#34;a&#34;</span><span style="color:#669933;">;</span><br><span style="color:#336699;">?></span><br><br> b.php<br><span style="color:#336699;"><?php</span><br><span style="color:#669933;">echo </span><span style="color:#cc3333;">&#34;b&#34;</span><span style="color:#669933;">;<br> exit;</span><br><span style="color:#336699;">?></span><br><br> (executing a.php:) will echo &#34;b&#34;.</span></code></span>
   </div>
</div>
</div>
<div id="112515" style="margin-left:0;">
<div>
   <div id="Vu112515">
    <ahref="https://www.php.net/manual/vote-note.php?id&#61;112515&page&#61;function.return&vote&#61;up">向上</a>
   </div>
   <div id="Vd112515">
    <ahref="https://www.php.net/manual/vote-note.php?id&#61;112515&page&#61;function.return&vote&#61;down">向下</a>
   </div>
   <div id="V112515">
    <span style="color:#333333;">74</span>
   </div>
</div>
<ahref="https://www.php.net/manual/zh/function.return.php#112515">JD 格莱姆斯</a>
<ahref="https://www.php.net/manual/zh/function.return.php#112515">¶</a>
<div style="text-align:right;">
   <span style="color:#666666;"><strong>8 年前</strong></span>
</div>
<div id="Hcom112515">
   <div style="text-align:left;">
    <span style="background-color:#ffffff;"><code><span style="color:#000000;">Note that because PHP processes the file before running it, any functions defined in an included file will still be available, even if the file is not executed.<br><br> Example:<br><br> a.php<br><span style="color:#336699;"><?php</span><br><span style="color:#669933;">include </span><span style="color:#cc3333;">'b.php'</span><span style="color:#669933;">;</span><br><br><span style="color:#336699;">foo</span><span style="color:#669933;">();</span><br><span style="color:#336699;">?></span><br><br> b.php<br><span style="color:#336699;"><?php</span><br><span style="color:#669933;">return;<br><br> function </span><span style="color:#336699;">foo</span><span style="color:#669933;">() {<br>      echo </span><span style="color:#cc3333;">'foo'</span><span style="color:#669933;">;<br> }</span><br><span style="color:#336699;">?></span><br><br> Executing a.php will output &#34;foo&#34;.</span></code></span>
   </div>
</div>
</div>
</div>
                </div>
      </div>
      <div id="treeSkill"></div>
页: [1]
查看完整版本: return将程序控制返还给调用模块