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: 2016-12-30 23:13 UTC
From: claudio dot frizziero at nereal dot com Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 5.0.5 OS: Fedora Core 4
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-12-30 23:13 UTC] cmb@php.net
-Package: Feature/Change Request +Package: Scripting Engine problem
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC