简体   繁体   English

为什么我在这里得到“意外令牌&”?

[英]Why am I getting an “unexpected token &” here?

My PHP syntax highlighter / intellisense is telling me that the & of &$cases in the line 我的PHP语法荧光笔/ intellisense告诉我行中的& of &$cases

$thisTable = $work_type === WorkTypes::Study ? &$cases : &$projs);

is an unexpected token. 是一个意外的令牌。 What I'm trying to do is create an alias for either my $cases or $projs object depending on whether $work_type === WorkTypes::Study . 我要尝试的是根据$work_type === WorkTypes::Study$cases$projs对象创建一个别名。

您缺少括号:

$thisTable = ($work_type === WorkTypes::Study ? &$cases : &$projs);

有时候,遗失了(括号可能会杀死您,这对我们来说是最好的。

$thisTable = ($work_type === WorkTypes::Study ? &$cases : &$projs);

The highlighter is right: the reference operator does not work inside a ternary operator because the =& is atomic (although the spaces don't seem to matter too much). 荧光笔是正确的:引用运算符在三元运算符内部不起作用,因为=&是原子的(尽管空格似乎无关紧要)。 Use this instead: 使用此代替:

$work_type === WorkTypes::Study ? $thisTable = &$cases : $thisTable = &$projs;

(and as others have mentioned: either no brackets or both) (正如其他人所提到的:要么没有括号,要么都有)

See also this SO answer . 另请参阅此SO答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM