php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29271 behaviour of $string['text'] = 'new string';
Submitted: 2004-07-20 01:54 UTC Modified: 2004-07-28 13:49 UTC
From: nospam0 at malkusch dot de Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.0.0 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
37 - 7 = ?
Subscribe to this entry?

 
 [2004-07-20 01:54 UTC] nospam0 at malkusch dot de
Description:
------------
$string = 'a bla'; 
$string['index'] = 'b blub'; 
 
does the same work as 
 
$string = 'a bla'; 
$string{(int)'index'} = 'b blub'{0}; 
 
without any notic of undefined index or too long replacing 
string. But it should throw any notice, because one could 
expect that the code should produce a new Array('index' => 
'b blubb'). 
 

Reproduce code:
---------------
$string = 'a bla';
$string['index'] = 'b blub';
var_dump($string);

Expected result:
----------------
Either notices for use of deprecated [], undefined offset 
'index' and too long replace char (and of course the 
actual result) 
 
or 'a bla' is deleted and $string has array('index' => 'b 
blubb') as value. 

Actual result:
--------------
string(5) "b bla" 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-28 12:39 UTC] tony2001@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

It's expected behaviour: 'index' is casted to it's integer value (0), that's why you get this result.
 [2004-07-28 13:15 UTC] nospam0 at malkusch dot de
> It's expected behaviour: 'index' is casted to it's  
> integer value (0), that's why you get this result. 
 
But as I use $string[] and not $string{} it's undefined 
wether it should be a new array or a string. So why 
doesn't PHP throw an error?
 [2004-07-28 13:25 UTC] tony2001@php.net
$string already IS a string and you still can use array-brackets to access a particular symbol, so it won't become an array.
 [2004-07-28 13:49 UTC] nospam0 at malkusch dot de
> $string already IS a string and you still can use 
> array-brackets to access a particular symbol 
 
So PHP will never throw an error, even [] on strings is 
deprecated since PHP4?
 [2004-07-28 14:30 UTC] m dot ford at leedsmet dot ac dot uk
Why should it?  It's only deprecated, not obsolete -- and there's not even a hint of a date being scheduled for its withdrawal.

(Deprecated meaning, roughly, not encouraged and may be withdrawn one day, maybe, if the php gurus ever get around to it.)

As withdrawal of this feature hasn't been announced yet, I think you can take it it'll probably be around at least for the lifetime of PHP 5 -- but the use of {} instead of [] is strongly encouraged.

Cheers!

Mike
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 04:01:27 2024 UTC