首页 / PHP / PHP中return的用法
PHP中return的用法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP中return的用法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1551字,纯文字阅读大概需要3分钟。
内容图文
今天研究了一下CI框架的使用,注意到他的配置文件,采用一种写法。如下:/**
* 注释若干
* 以下是一个格式如config.php的文件
*/
return array(
'config1' => 'some value',
'config2' => 'some value',
);
?>
在这个文件中,直接就写了一个return,这个用法又一次突破了我的常识。特意查询了一下文档,里面这样描述的:
return
If called from within a function, the return() statement immediately ends execution of the current function, and returns its argument as the value of the function call. return() will also end the execution of an eval() statement or script file.
If called from the global scope, then execution of the current script file is ended. If the current script file was include()ed or require()ed, then control is passed back to the calling file. Furthermore, if the current script file was include()ed, then the value given to return() will be returned as the value of the include() call. If return() is called from within the main script file, then script execution ends. If the current script file was named by the auto_prepend_file or auto_append_file configuration options in php.ini, then that script file's execution is ended.
return语句可以终止函数执行那自不必说了,这里还提到了可以终止eval过程的进行,并且如果处于被include的文件中,还能使return的值成为include和require函数的返回值。这样写的好处是,一个语句就可以得到配置项的内容了。
//原来这样写
require './config.php';
function test() {
global $config;
if ($config['a']=='b') echo 'hello';
}
//现在
function test() {
$config = require('./config.php');
if ($config['a']=='b') echo 'hello';
}
?>
内容总结
以上是互联网集市为您收集整理的PHP中return的用法全部内容,希望文章能够帮你解决PHP中return的用法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。