php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79053 No error is reported when using null coalescing operator
Submitted: 2020-01-01 13:27 UTC Modified: 2020-01-01 14:02 UTC
From: rubencm at gmail dot com Assigned:
Status: Closed Package: *General Issues
PHP Version: 7.4.1 OS:
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: rubencm at gmail dot com
New email:
PHP Version: OS:

 

 [2020-01-01 13:27 UTC] rubencm at gmail dot com
Description:
------------
When I try to access to an array offset on a boolean, a notice (a warning in master) is thrown.

<?php
false['']; // php notice: "Trying to access array offset on value of type bool"
?>

But when I use the null coalescing operator with it, that error isn't reported

<?php
false['']??false; // false, no error
?>

I'm not sure, but I think it's not the expected behavior, since possible errors are hidden.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-01-01 13:28 UTC] bugreports at gmail dot com
taht's the whole point of null coalescing operator
 [2020-01-01 13:37 UTC] rubencm at gmail dot com
Well, in this case the notice is being reported, that still don't have much sense for me.

<?php
function bar()
{
    trigger_error("foo", E_USER_NOTICE);
    
    return null;
}
var_dump(bar()??false);
 [2020-01-01 13:41 UTC] rubencm at gmail dot com
Here is an equivalent codes with diferent behavior

<?php
function bar()
{
    return false[''];
}
var_dump(bar()??false);
?>

<?php
var_dump(false['']??false);
?>
 [2020-01-01 13:44 UTC] bugreports at gmail dot com
you can't seriously compare that becaus ethe case is completly wrong

trigger_error("foo", E_USER_NOTICE); has nothing to do with "null coalescing operator" at all, you call the function and the function does what it is supposed to do, throw a E_USER_NOTICE

why do you think "null coalescing operator" is involved here at all?
teh function returns NULL and "bar()??false" does what it is supposed to do
 [2020-01-01 13:48 UTC] bugreports at gmail dot com
<?php
function bar()
{
    return false[''];
}
?>

this has again *nothing* to do with "null coalescing operator" because you are not using it at all, that way you would usie it

<?php
function bar()
{
    return false[''] ?? NULL;
}

that all looks like lack of understand php basics
?>
 [2020-01-01 13:52 UTC] rubencm at gmail dot com
Well, just close the report if you want.
But I thought that all this cases should have the same behavior, while in the last line the error isn't being reported.
If is the expected behavior just close this.

<?php
function bar()
{
    return false[''];
}
bar();
false[''];

bar()??false;
false['']??false;
 [2020-01-01 13:57 UTC] bugreports at gmail dot com
> But I thought that all this cases should have the same behavior

they have but you compare different cases

-------------------

Notice: Trying to access array offset on value of type bool in /mnt/data/downloads/test.php on line 4

line 4 don't use coalescing operator at all
return false[''];

-------------------

Notice: Trying to access array offset on value of type bool in /mnt/data/downloads/test.php on line 7

line 7 don't use coalescing operator at all
false[''];

-------------------

this is line 9 and don#t throw a warning
bar()??false;

Notice: Trying to access array offset on value of type bool in /mnt/data/downloads/test.php on line 4

would you mind which line trhows the error?
it happens inside line 4 again which *does not use* null coalescing operator at all
 [2020-01-01 14:00 UTC] bugreports at gmail dot com
this would be your last example using null coalescing operator
again: lack of basic understanding how php works!

<?php
function bar()
{
  return false[''] ?? NULL;
}
bar();
false[''] ?? NULL;

bar()??false;
false['']??false;
 [2020-01-01 14:02 UTC] rubencm at gmail dot com
-Status: Open +Status: Closed
 [2020-01-01 14:02 UTC] rubencm at gmail dot com
expected behavior
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 14:00:01 2026 UTC