|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2005-10-13 05:28 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 12:00:01 2025 UTC | 
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.