简体   繁体   中英

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

My PHP syntax highlighter / intellisense is telling me that the & of &$cases in the line

$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 .

您缺少括号:

$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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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