Req #34848 [Opn->Csd]: Behaviour of array brackets with strings

From: Date: Thu, 09 Jan 2020 11:33:44 +0000
Subject: Req #34848 [Opn->Csd]: Behaviour of array brackets with strings
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-224807@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=34848&edit=1 ID: 34848 Updated by: nikic@php.net Reported by: claudio dot frizziero at nereal dot com Summary: Behaviour of array brackets with strings -Status: Open +Status: Closed Type: Feature/Change Request Package: Scripting Engine problem Operating System: Fedora Core 4 PHP Version: 5.0.5 -Assigned To: +Assigned To: nikic Block user comment: N Private report: N New Comment: A couple of points: * $string[$offset] is not a legacy syntax. In fact, $x{$y} is the legacy syntax and deprecated as of 7.4. * $int[$x] and friends throw a notice in 7.4, a warning in 8.0, so I believe that is addressed. * $string[$string] (where $string is not an integer string) also throws a warning since a long time ago, sot that is also addressed. With that, I don't think there's really anything left to do here. Previous Comments: ------------------------------------------------------------------------ [2005-10-13 05:28:47] claudio dot frizziero at nereal dot com Description: ------------ Array brackets can be used, for backwards compatibility, to specify a zero-based offset of a char on a string. However, current implementation is ambiguous and forces to use dirty workaround to avoid the problem - if you realize that it doesn't work as you imagine. The ambiguity resides on the different behaviour of the same syntax using different types of keys and values: - $string[$int] works like $string{$int} - $int[$int] gives NULL - $string[$string] works like $string{0} - $int[$string] gives NULL My request is: - add a directive into the configuration file to disable the "compatibility mode" of array brackets (just because it's deprecated: I can test my script before it will definitively changed) or - alternately, as a temporary workaround: - if the type of key is string, parser assumes that is a key, not an offset Thank you for taking the time to read me. =) Reproduce code: --------------- $t = 'abc'; var_dump ($t[1]); var_dump ($t['key']); $t = 123; var_dump ($t[1]); var_dump ($t['key']); Expected result: ---------------- NULL NULL NULL NULL Actual result: -------------- string(1) "b" string(1) "a" NULL NULL ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=34848&edit=1

« previous php.bugs (#224807) next »