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;">(PHP 4、PHP 5、PHP 7、PHP 8)</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> 语句,将立即结束此函数的执行并将它的参数作为函数的值返回。<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;">如果在全局范围中调用,则当前脚本文件中止运行。如果当前脚本文件是被 <ahref="https://www.php.net/manual/zh/function.include.php">include</a> 的或者 <ahref="https://www.php.net/manual/zh/function.require.php">require</a> 的,则控制交回调用文件。此外,如果当前脚本是被 <ahref="https://www.php.net/manual/zh/function.include.php">include</a> 的,则 <strong>return</strong> 的值会被当作 <ahref="https://www.php.net/manual/zh/function.include.php">include</a> 调用的返回值。如果在主脚本文件中调用 <strong>return</strong>,则脚本中止运行。如果当前脚本文件是在 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> 所指定的,则此脚本文件中止运行。</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> 是语言结构而不是函数,因此其参数没有必要用括号将其括起来,也不推荐这样用。</span></p>
</blockquote>
<blockquote>
<p style="margin-left:0;"><span style="color:#333333;"><strong>注意</strong>: 如果没有提供参数,则一定不能用括号,此时返回 <strong><code>null</code></strong>。如果调用 <code>return</code> 时加上了括号却又没有参数会导致解析错误。</span></p>
</blockquote>
<p style="margin-left:0;"><span style="color:#333333;">自 PHP 7.1.0 起,如果返回类型需要是 void 而带了返回的参数, 将导致 <strong><code>E_COMPILE_ERROR</code></strong>; 相反返回类型需要而未带参数也会同样导致该错误。</span></p>
</div>
<div>
<ahref="https://www.php.net/manual/add-note.php?sect=function.return&redirect=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=59866&page=function.return&vote=up">向上</a>
</div>
<div id="Vd59866">
<ahref="https://www.php.net/manual/vote-note.php?id=59866&page=function.return&vote=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;">"b.php"</span><span style="color:#669933;">);<br> echo </span><span style="color:#cc3333;">"a"</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;">"b"</span><span style="color:#669933;">;<br> return;</span><br><span style="color:#336699;">?></span><br><br> (executing a.php:) will echo "ba".<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;">"b.php"</span><span style="color:#669933;">);<br> echo </span><span style="color:#cc3333;">"a"</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;">"b"</span><span style="color:#669933;">;<br> exit;</span><br><span style="color:#336699;">?></span><br><br> (executing a.php:) will echo "b".</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=112515&page=function.return&vote=up">向上</a>
</div>
<div id="Vd112515">
<ahref="https://www.php.net/manual/vote-note.php?id=112515&page=function.return&vote=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 "foo".</span></code></span>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="treeSkill"></div>
页:
[1]