php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #34848 Behaviour of array brackets with strings
Submitted: 2005-10-13 05:28 UTC Modified: 2020-01-09 11:33 UTC
From: claudio dot frizziero at nereal dot com Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.0.5 OS: Fedora Core 4
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: claudio dot frizziero at nereal dot com
New email:
PHP Version: OS:

 

 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-12-30 23:13 UTC] cmb@php.net
-Package: Feature/Change Request +Package: Scripting Engine problem
 [2020-01-09 11:33 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2020-01-09 11:33 UTC] nikic@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Nov 24 16:01:31 2024 UTC