php 提示Only variables should be assigned by reference?
大家讲道理
大家讲道理 2017-04-11 09:18:43
[PHP讨论组]

Strict Standards: Only variables should be assigned by reference

函数的返回值似乎不能引用

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(5)
PHP中文网

当前php版本不影响功能错误提示,关闭错误提示就可以了,但不保证在将来的版本不失效。

或者修改成这样就不会报错了

function &getArr(){
    static $arr = [3,4,6];
    return $arr;
}

$arr2 = &getArr();
$arr2[0] = 100;
var_dump($arr2);

$arr3 = &getArr();
var_dump($arr3);
高洛峰

一般不要使用函数返回引用,详见php官方文档

Do not use return-by-reference to increase performance. The engine will automatically optimize this on its own. Only return references when you have a valid technical reason to do so.

不要企图通过返回引用提高程序性能,引擎会自动进行优化,只有在有明确的里有的情况下才可以使用!

这里之所以报警告,是因为用法错误

Note: Unlike parameter passing, here you have to use & in both places - to indicate that you want to return by reference, not a copy, and to indicate that reference binding, rather than usual assignment, should be done for $myValue.

也就是说必须要同时在函数定义和使用的时候前面都加&,正如 @咪蛾 所说 。

PHP中文网

没毛病啊 可以输出,都是变量引用同一个地址

天蓬老师

是同一指针地址,应该没有错

array(3) { [0]=> int(100) [1]=> int(4) [2]=> int(6) }
伊谢尔伦

从编程角度来讲,还是不要将局部变量引用出来的好。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号