使用三元运算符时可以通过引用传递吗?

发布于 2024-09-12 07:50:59 字数 308 浏览 7 评论 0原文

简单的问题,简单的代码。这有效:

$x = &$_SESSION['foo'];

这不起作用:

$x = (isset($_SESSION['foo']))?&$_SESSION['foo']:false;

它抛出 PHP 解析错误:语法错误,意外的“&”。使用条件运算符时是否无法通过引用传递,为什么不呢?如果 ?& 之间有空格,也会发生这种情况。

Simple question, simple code. This works:

$x = &$_SESSION['foo'];

This does not:

$x = (isset($_SESSION['foo']))?&$_SESSION['foo']:false;

It throws PHP Parse error: syntax error, unexpected '&'. Is it just not possible to pass by reference while using the conditional operator, and why not? Also happens if there's a space between the ? and &.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鹿! 2024-09-19 07:50:59

在非常简单的情况下,这个表达式是非法的;

$c = condition ? &$a : &$b; // Syntax error

可以这样写:

$c = &${ condition ? 'a' : 'b' };

在您的具体情况下,由于如果条件为假,您不会通过引用分配,所以更好的选择似乎是:

$x = isset($_SESSION['foo']) ? $x = &$_SESSION['foo'] : false;

In the very simply case, this expression, which is illegal;

$c = condition ? &$a : &$b; // Syntax error

can be written like this:

$c = &${ condition ? 'a' : 'b' };

In your specific case, since you're not assigning by reference if the condition is false, a better option seems to be:

$x = isset($_SESSION['foo']) ? $x = &$_SESSION['foo'] : false;
一梦浮鱼 2024-09-19 07:50:59

简单的回答:不。你必须用 if/else 绕很远的路。一次有参考,下一次有值的情况也很少见,而且可能会造成混乱。我会发现这更直观,但话又说回来,我当然不知道你的代码:

if(!isset($_SESSION['foo'])) $_SESSION['foo'] = false;
$x = &$_SESSION['foo'];

至于为什么:不知道,可能必须在解析器认为某些东西是值的副本或引用的创建时,这样就无法在解析时确定。

Simple answer: no. You'll have to take the long way around with if/else. It would also be rare and possibly confusing to have a reference one time, and a value the next. I would find this more intuitive, but then again I don't know your code of course:

if(!isset($_SESSION['foo'])) $_SESSION['foo'] = false;
$x = &$_SESSION['foo'];

As to why: no idea, probably it has to with at which point the parser considers something to be an copy of value or creation of a reference, which in this way cannot be determined at the point of parsing.

徒留西风 2024-09-19 07:50:59

让我们试试:

$x =& true?$y:$x;
Parse error: syntax error, unexpected '?', expecting T_PAAMAYIM_NEKUDOTAYIM in...
$x = true?&$y:&$x;
Parse error: syntax error, unexpected '&' in...

所以,你看,它甚至没有解析。对于为什么不允许这样做,维基百科可能是正确的。

你可以用一个函数来解决这个问题:

function &ternaryRef($cond, &$iftrue, &$iffalse=NULL) {
    if ($cond)
        return $iftrue;
    else
        return $iffalse;
}

$x = 4;
$a = &ternaryRef(true, $x);
xdebug_debug_zval('a');
$b = &ternaryRef(false, $x);
xdebug_debug_zval('b');

给出:

a:

(refcount=2, is_ref=1),int 4

b:

(refcount=1, is_ref=0),null

Let's try:

$x =& true?$y:$x;
Parse error: syntax error, unexpected '?', expecting T_PAAMAYIM_NEKUDOTAYIM in...
$x = true?&$y:&$x;
Parse error: syntax error, unexpected '&' in...

So, you see, it doesn't even parse. Wikken is probably right as to why it's not allowed.

You can get around this with a function:

function &ternaryRef($cond, &$iftrue, &$iffalse=NULL) {
    if ($cond)
        return $iftrue;
    else
        return $iffalse;
}

$x = 4;
$a = &ternaryRef(true, $x);
xdebug_debug_zval('a');
$b = &ternaryRef(false, $x);
xdebug_debug_zval('b');

gives:

a:

(refcount=2, is_ref=1),int 4

b:

(refcount=1, is_ref=0),null

止于盛夏 2024-09-19 07:50:59

不幸的是,你不能。

$x=false;
if (isset($_SESSION['foo']))
   $x=&$_SESSION['foo'];

Unfortunately, you can't.

$x=false;
if (isset($_SESSION['foo']))
   $x=&$_SESSION['foo'];
人生戏 2024-09-19 07:50:59

对此错误报告的评论可能会阐明该问题:
http://bugs.php.net/bug.php?id=53117

本质上,尝试从三元运算符的结果分配引用的两个问题是:

  1. 表达式不能产生引用,并且
  2. $x = (expression) 不是引用分配,即使(expression) 是一个引用(实际上不是;参见第 1 点)。

The commentary on this bug report might shed some light on the issue:
http://bugs.php.net/bug.php?id=53117.

In essence, the two problems with trying to assign a reference from the result of a ternary operator are:

  1. Expressions can't yield references, and
  2. $x = (expression) is not a reference assignment, even if (expression) is a reference (which it isn't; see point 1).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文