php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73919 Short ternary operator and reference
Submitted: 2017-01-12 12:54 UTC Modified: 2017-01-12 13:37 UTC
From: p17 at ivaldi dot xyz Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.1.0 OS: Linux 4.8.0-2-amd64
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: p17 at ivaldi dot xyz
New email:
PHP Version: OS:

 

 [2017-01-12 12:54 UTC] p17 at ivaldi dot xyz
Description:
------------
Using the short tertiary operator ?: with a pointer as first condition results in a non expected result.

Test script:
---------------
#!/usr/bin/php
<?php

error_reporting(E_ALL | E_STRICT);

$val = &$foo ?: 'default';

var_dump($val);

Expected result:
----------------
string(7) "default"

Actual result:
--------------
NULL

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-01-12 13:13 UTC] cmb@php.net
-Summary: Short tertiary operator and pointer +Summary: Short ternary operator and reference -Status: Open +Status: Verified -Package: *General Issues +Package: Scripting Engine problem
 [2017-01-12 13:13 UTC] cmb@php.net
Confirmed: <https://3v4l.org/IU2Cq#v560>.
 [2017-01-12 13:37 UTC] nikic@php.net
-Status: Verified +Status: Not a bug
 [2017-01-12 13:37 UTC] nikic@php.net
Your code is equivalent to ($val =& $foo) ?: 'default' and behaves accordingly. The RHS of a by-reference assignment must be variable-like, so this is the only valid way of parsing the code.

We could relax the RHS to be an expression and impose the variable-like constraint subsequently. However, this would be a backwards-incompatible change. The current parsing is consistent with our treatment of variables in other circumstances, e.g. !$x instanceof y parses as !($x instanceof y) rather than (!$x) instanceof y, because the LHS of instanceof must be variable-like.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 22:00:02 2026 UTC