php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75209 Bug in ??
Submitted: 2017-09-14 15:17 UTC Modified: 2017-09-14 15:23 UTC
From: ynijar at hotmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.0.23 OS: Windows/Linux
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: ynijar at hotmail dot com
New email:
PHP Version: OS:

 

 [2017-09-14 15:17 UTC] ynijar at hotmail dot com
Description:
------------
        $hash = null;
        $hash ?? 'test'; // output 'test'

        $hash = '';
        $hash ?? 'test'; // output '' - WHY????

        $hash = "";
        !$hash ? 'test' : $hash; // output 'test'

Test script:
---------------
        $hash = null;
        $hash ?? 'test'; // output 'test'

        $hash = '';
        $hash ?? 'test'; // output '' - WHY????

        $hash = "";
        !$hash ? 'test' : $hash; // output 'test'

Expected result:
----------------
        $hash = null;
        $hash ?? 'test'; // output 'test'

        $hash = '';
        $hash ?? 'test'; // output 'test' - it's normal

        $hash = "";
        !$hash ? 'test' : $hash; // output 'test'


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-09-14 15:23 UTC] daverandom@php.net
-Status: Open +Status: Not a bug
 [2017-09-14 15:23 UTC] daverandom@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This is the expected behaviour for ??, which is specifically the NULL coalesce operator.

The shorthand ternary operator ?: gives the behaviour you are expecting.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 19:00:01 2026 UTC