• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PHP中return的用法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

<?php

/**
* 注释若干
* 以下是一个格式如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';
}
?>


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php脚本执行时间限制发布时间:2022-07-10
下一篇:
laravel使用路由api.php_fei发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap