Bug #81212 [Nab]: Throws a Notice exception when the [Null Coalescing Operator] detects an array

From: Date: Thu, 01 Jul 2021 07:26:09 +0000
Subject: Bug #81212 [Nab]: Throws a Notice exception when the [Null Coalescing Operator] detects an array
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-234718@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81212&edit=1 ID: 81212 Updated by: requinix@php.net Reported by: su at zorzz dot com Summary: Throws a Notice exception when the [Null Coalescing Operator] detects an array Status: Not a bug Type: Bug Package: *General Issues Operating System: linux(CentOS7.2) PHP Version: 7.3.28 Block user comment: N Private report: N New Comment: ?? 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. Previous Comments: ------------------------------------------------------------------------ [2021-07-01 07:20:14] 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:07:48] 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 06:22:14] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=81212&edit=1

« previous php.bugs (#234718) next »