php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #33879 $string["otherstring"] = something | should produce a warning
Submitted: 2005-07-27 01:47 UTC Modified: 2014-11-01 23:44 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: sigge at hystrix dot se Assigned: requinix (profile)
Status: Closed Package: *General Issues
PHP Version: 5.0.4 OS:
Private report: No CVE-ID: None
 [2005-07-27 01:47 UTC] sigge at hystrix dot se
Description:
------------
I recently had a bug in my code that overwrote an array with a string. I missed the [], it happens.
But debugging was hard, and I had a bunch of $array["foo"] = "bar"; when $array had been overwritten by a string.
As kind people on #php explained to me, PHP treats any string as 0. While I can understand such behaviour, I think there should be a warning like "you are treating a string as an array" or something.

This warning should probably not occur when the key is a number, as this would return the character, but when accessing a string key of a string makes no sense.

I can't see how this would break any backward compatibility, and it would make debugging much easier! 

Edit: I've now seen that there are similar bugs, all marked closed and without any non-stock explanation. 
I would really like to know why this "is expected", because to me, having developed in PHP for two years, this wasn't expected at all, and caused me a good headache.
Pardon me if I am spamming, I would prefer to add a comment to those bugs, but can't.

Reproduce code:
---------------
$array = array();
$array = "string";
$array["foo"] = "bar";

Expected result:
----------------
Would be nice if it produced a warning on line 3, "Trying to access a string as an array"


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-11-01 23:44 UTC] requinix@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: requinix
 [2014-11-01 23:44 UTC] requinix@php.net
PHP issues a warning "Illegal string offset '<offset>'" as of 5.4.
http://3v4l.org/dj9pf
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 06:01:35 2024 UTC