三元运算符未在PHP中使用引用变量 [英] Ternary operator not working with reference variables in PHP

查看:90
本文介绍了三元运算符未在PHP中使用引用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不起作用?

$a = 'FOO';

$foo = $a ? &$a : 'whatever'; // <- error here

echo $foo;

我收到解析错误:|

推荐答案

如果要使用三元语句分配引用,则需要这种笨拙的解决方法:

If you want to assign a reference using a ternary statement, then you need this clumsy workaround:

 list($foo) = $a ? array(&$a) : array('whatever');

但是,正如其他答案所述,这很少节省内存。

However, as said in the other answers, this seldomly saves memory.

这篇关于三元运算符未在PHP中使用引用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆