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
 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 20:01:32 2024 UTC