|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76707 PHP Parse error: syntax error, unexpected '&'
Submitted: 2018-08-05 00:24 UTC Modified: 2018-08-05 00:30 UTC
From: ASchmidt at Anamera dot net Assigned:
Status: Not a bug Package: Variables related
PHP Version: 7.2.8 OS: Win64
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
13 + 42 = ?
Subscribe to this entry?

 [2018-08-05 00:24 UTC] ASchmidt at Anamera dot net
One can use an if/then/else construct to conditionally assign a reference to a variable. However, the more compact/readable assignment by means a ternary operator, rejects the "&" before the variable.

Test script:

$myArray = array( [ 'pear', 'sweet' ], [ 'orange', 'sour' ] );

if ( isset( $myArray [5] ) )
    $entryRef = &$myArray [5];
    $entryRef = &$myArray [1];
echo $entryRef[1];  // works

// Syntax error:
$entryRef = isset( $myArray [5] ) ? &$myArray[5] : &$myArray[1];

Expected result:
$entryRef = &$variable; 
is a valid assignment - so it seems counterintuitive for the similar assignment

$entryRef = condition ? &$variable1 : NULL;
to be "invalid syntax".

Actual result:
PHP Parse error:  syntax error, unexpected '&'


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2018-08-05 00:30 UTC]
-Status: Open +Status: Not a bug
 [2018-08-05 00:30 UTC]
& is not an operator. It is a symbol used to modify certain actions, like with assignment means to assign by reference, or in a function parameter list means that an argument is passed by reference.

The ternary is an expression, and like other expressions you cannot use references with them.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 25 18:01:32 2024 UTC