宜信同城网交流论坛

 找回密码
 立即注册
开启左侧

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

[复制链接]
发表于 2023-2-16 18:54:50 | 显示全部楼层 |阅读模式 来自 LAN

(PHP 4、PHP 5、PHP 7、PHP 8)

return将程序控制返还给调用模块。 将在调用模块中执行的下一句表达式中继续。

如果在一个函数中调用 return 语句,将立即结束此函数的执行并将它的参数作为函数的值返回。return 也会终止 eval() 语句或者脚本文件的执行。

如果在全局范围中调用,则当前脚本文件中止运行。如果当前脚本文件是被 include 的或者 require 的,则控制交回调用文件。此外,如果当前脚本是被 include 的,则 return 的值会被当作 include 调用的返回值。如果在主脚本文件中调用 return,则脚本中止运行。如果当前脚本文件是在 php.ini 中的配置选项 auto_prepend_file 或者 auto_append_file 所指定的,则此脚本文件中止运行。

更多信息见返回值

注意: 注意既然 return 是语言结构而不是函数,因此其参数没有必要用括号将其括起来,也不推荐这样用。

注意: 如果没有提供参数,则一定不能用括号,此时返回 null。如果调用 return 时加上了括号却又没有参数会导致解析错误。

自 PHP 7.1.0 起,如果返回类型需要是 void 而带了返回的参数, 将导致 E_COMPILE_ERROR; 相反返回类型需要而未带参数也会同样导致该错误。

正在上传…重新上传取消 添加注释

用户贡献的笔记2 个音符

warhog 在 warhog dot net  ¶
16年前
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.

look at that example:

a.php

include("b.php");
echo 
"a";
?>

b.php

echo "b";
return;

?>

(executing a.php:) will echo "ba".

whereas (b.php modified):

a.php

include("b.php");
echo 
"a";
?>

b.php

echo "b";
exit;

?>

(executing a.php:) will echo "b".
JD 格莱姆斯
8 年前
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.

Example:

a.php

include 'b.php';

foo();
?>

b.php

return;

function 
foo() {
     echo 
'foo';
}

?>

Executing a.php will output "foo".
宜信网交流论坛 - 版权声明 1、在发表言论时,请遵守当地法律法规。主题所有言论纯属个人意见,与本站立场无关。
2、本站所有主题由作者发表,作者享有帖子相关版权,其他单位或个人使用、转载或引用本文时必须征得作者同意并注明来源于宜信网
3、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意。
4、帖子不遵守当地法律法规、广告、人身攻击等情况时,宜信网管理人员有权不事先通知发贴者而删除本文。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|宜信同城网交流论坛 ( 闽ICP备19021048号 )|站点地图

GMT+8, 2026-3-28 03:19 , Processed in 0.266770 second(s), 12 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表