php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #31303 treating a string like a regular array leads to a fatal offset
Submitted: 2004-12-26 08:07 UTC Modified: 2005-04-11 09:51 UTC
From: schmad at miller-group dot net Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.0.3 OS: Mac OS X 10.3.7
Private report: No CVE-ID:
 [2004-12-26 08:07 UTC] schmad at miller-group dot net
Description:
------------
Using array functions on a string like count() or 
unset() results in fatal offset errors which were not 
encountered in 4.3.x code.

Reproduce code:
---------------
$link = 'bla';
unset($link['one']);
//In 4.x: No Error


$link = '';
count($link['two']);
//In 4.x: No Error

Expected result:
----------------
$link = 'bla';
unset($link['one']);
//In 4.x: No Error


$link = '';
count($link['two']);
//In 4.x: No Error

Actual result:
--------------
$link = 'bla';
unset($link['one']);
//Outputs: Fatal error: Cannot unset string offsets in 
...


$link = '';
count($link['two']);
//Outputs: Fatal error: Cannot use string offset as an 
array in ...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-12-26 11:35 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This is expected, and not a bug. It doesn\'t make sense to do this at all either.
 [2004-12-26 20:08 UTC] schmad at miller-group dot net
Perhaps it isn't a bug, but it does break backward 
compatibility with 4.3.
 [2004-12-26 20:29 UTC] derick@php.net
THen it should be documented.
 [2004-12-26 22:29 UTC] nlopess@php.net
opening..
 [2004-12-29 16:43 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

I believe it is already documented in Backward Incompatible Changes under Migrating from PHP 4 to PHP 5 in the sentence "Illegal use of string offsets causes E_ERROR instead of E_WARNING."

However, I have added this to the String Type chapter: "However, this syntax is deprecated as of PHP 4 and produces fatal error in PHP 5."
 [2004-12-29 17:08 UTC] et@php.net
You changed the wrong thing, $string[0] still works.
et@edea:~$ php -r '$foo = "abc"; var_dump($foo[0]);'
string(1) "a"
Also, the second example doesn't seem to produce a fatal error.
et@edea:~$ php -r '$link = ""; var_dump(count($link["two"]));'

Notice: Uninitialized string offset:  0 in Command line code on line 1
int(1)
et@edea:~$ php -v
PHP 5.1.0-dev (cli) (built: Dec 28 2004 20:03:13)

 [2004-12-29 20:47 UTC] nlopess@php.net
Yep, please revert the change at 'language.types.string'.
PHP 5 only causes a fatal error whith 'unset($string["a"]);'

Nuno
 [2004-12-30 09:48 UTC] vrana@php.net
Reverted, sorry. I'm not sure where to add this piece of information so I'm leaving this bug open for someone else :-).
 [2005-04-11 09:51 UTC] philip@php.net
This behavior isn't limited to [] either, {} is affected the same way. But anyway, using unset() on strings like this has no use whatsoever.

An example has now been added to migration5.xml so this bug is closed.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Mon Apr 21 02:02:11 2014 UTC