php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52868 string variable array acces with string index should emit E_NOTICE
Submitted: 2010-09-17 11:56 UTC Modified: 2010-09-19 18:05 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: sht dot alien at gmx dot net Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.3.3 OS: Ubuntu Linux 10.04 x64
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: sht dot alien at gmx dot net
New email:
PHP Version: OS:

 

 [2010-09-17 11:56 UTC] sht dot alien at gmx dot net
Description:
------------
It sometimes happens that you accidentially acces a string via array access and be so unfortunate to have used a string index :-)
Since I see no point in using stingindexes for accessing strings via array access at any time, I propose to issue an E_NOTICE when a string index is cast to integer for array access (on strings), if that is possible.

Test script:
---------------
<?php
$stringValue = 'foobar';

echo $stringValue['baz']; // echoes 'f' since 'baz' is cast to int 0
echo $stringValue['3baz']; // echoes 'b' since the first character of the index string is 3, thus the index string is cast to int 3
echo $stringValue['ba3z']; // echoes 'f' since 'ba3z' is cast to int 0
?>

Expected result:
----------------
f
b
f


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-09-19 18:05 UTC] markskilbeck@php.net
-Status: Open +Status: Bogus
 [2010-09-19 18:05 UTC] markskilbeck@php.net
I don't see that this makes sense.

PHP expects an integer for the key, and PHP being PHP, it will just juggle the type if it can. The PHP Way(tm).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC