PHP中return的用法

今天研究了一下CI框架的使用,注意到他的配置文件,采用一种写法。如下:


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_r() 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';
}
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值