php.net |  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 Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ASchmidt at Anamera dot net
New email:
PHP Version: OS:

 

 [2018-08-05 00:24 UTC] 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 '&'

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-08-05 00:30 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-08-05 00:30 UTC] requinix@php.net
& 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: Fri Dec 27 00:01:30 2024 UTC