php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81212 Throws a Notice exception when the [Null Coalescing Operator] detects an array
Submitted: 2021-07-01 06:22 UTC Modified: 2021-07-01 07:55 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: su at zorzz dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.3.28 OS: linux(CentOS7.2)
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: su at zorzz dot com
New email:
PHP Version: OS:

 

 [2021-07-01 06:22 UTC] su at zorzz dot com
Description:
------------
Null Coalescing Operator throw a notice when key is a numeric string, just like "10-10" or "123b".

Test script:
---------------
<?php
error_reporting(E_ALL);
date_default_timezone_set("Asia/Shanghai");

$context = ['attributes' => 'config'];

// example1
var_dump($context['attributes']['10-10'][':type'] ?? null);

// the same as example1
var_dump(isset($context['attributes']['10-10'][':type']) ? $context['attributes']['10-10'][':type'] : null);


Expected result:
----------------
NULL

NULL

// Isset function and [Null Coalescing Operator] should not throw exceptions.

Actual result:
--------------
Notice: A non well formed numeric value encountered in /home/zorzz/index.php on line 8
NULL

Notice: A non well formed numeric value encountered in /home/zorzz/index.php on line 9
NULL

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-07-01 07:07 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2021-07-01 07:07 UTC] nikic@php.net
Null coalesce is not a generic error suppression operator. The only error conditions it needs to suppress are "undefined index" style errors. For historical reasons, some additional errors may be suppressed, but that's generally something we're moving away from.
 [2021-07-01 07:20 UTC] su at zorzz dot com
But the PHP manual gives a similar use, and "10-10" is a correct index.
I'm amazed. I think the judgment behavior is inconsistent in some cases.
 [2021-07-01 07:26 UTC] requinix@php.net
?? will not hide trying to use an invalid *string offset*. Because $context['attributes'] is a string, not an array.

$array['10-10'] ?? null is okay.
$string['10-10'] ?? null is not okay.
 [2021-07-01 07:55 UTC] su at zorzz dot com
I got it.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 13 12:01:27 2024 UTC