Bug #76707 [Opn->Nab]: PHP Parse error: syntax error, unexpected '&'

From: Date: Sun, 05 Aug 2018 00:30:43 +0000
Subject: Bug #76707 [Opn->Nab]: PHP Parse error: syntax error, unexpected '&'
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-216588@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76707&edit=1 ID: 76707 Updated by: requinix@php.net Reported by: ASchmidt at Anamera dot net Summary: PHP Parse error: syntax error, unexpected '&' -Status: Open +Status: Not a bug Type: Bug Package: Variables related Operating System: Win64 PHP Version: 7.2.8 Block user comment: N Private report: N New Comment: & 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. Previous Comments: ------------------------------------------------------------------------ [2018-08-05 00:24:45] ASchmidt at Anamera dot net Description: ------------ 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: --------------- <?php declare(strict_types=1); $myArray = array( [ 'pear', 'sweet' ], [ 'orange', 'sour' ] ); if ( isset( $myArray [5] ) ) $entryRef = &$myArray [5]; else $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 '&' ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=76707&edit=1

« previous php.bugs (#216588) next »