php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52381 You're doing the wrong thing with string accessors
Submitted: 2010-07-20 01:53 UTC Modified: 2010-07-20 06:08 UTC
From: mbt at gator dot net Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.3.2 OS: Linux
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: mbt at gator dot net
New email:
PHP Version: OS:

 

 [2010-07-20 01:53 UTC] mbt at gator dot net
Description:
------------
I should have made more fuss about this earlier, but here it is now.

I think you are taking exactly the wrong approach to addressing single
characters in strings.  I am of the very strong opinion that the direct
addressing of characters in strings have an obvious syntactic difference from
the addressing of array elements.  This means you should keep {} and deprecate
[] for addressing characters in strings.

PHP is not C!!  In C, strings are arrays, so [] is the preferred C operator to
address single bytes (or character codes) in a string.  In PHP, strings are a
primitive type; they are not arrays.  The notation $foo{0} makes it plain that
$foo is a string, not an array and that you are accessing a byte, not an array
element.  Making this obvious through the syntax is more important than you
might think.

Imagine $bar to be a 3x3 array of strings.  In this case, what are you to
assume when you see in the middle of a script, far away from the assignment to
$bar, when you read
   $baz = $bar[$a][$b][$c];

If you have instead
   $baz = $bar[$a][$b]{$c};
you know you're not doing ordinary array addressing.

These issues become important for the people who have to maintain the code. 
Consider two more ways confusion can arise because strings are not arrays.

If $abc is a string,
   $d = $abc[2];
works but
   $abc[2] = array('boo');
fails.  The elements of a string "array" do not work like true array elements.

Indices to strings must be integers; unlike arrays, strings do not allow
associative indices.  This fails:
   $abc['key'] = 'def';

I believe that indicating single-byte access via [] is such a bad idea that if
you're dead set on removing {}, I would prefer to deprecate that kind of access
altogether in favor of substr() and friends.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-20 06:08 UTC] aharvey@php.net
-Status: Open +Status: Bogus
 [2010-07-20 06:08 UTC] aharvey@php.net
This decision dates back the best part of five years at this stage to
the developer meeting in November 2005. [0] It's come up a couple of
times on the Internals mailing list since then -- both immediately
after the developer meeting [1], and then to confirm the 5.3 migration
document was correct. [2]

To be blunt, the horse has already bolted on this. {} is deprecated,
and [] is the supported way to access strings by offset both now and
in the future.

[0] http://www.php.net/~derick/meeting-notes.html#cleanup-for-vs
[1] http://www.mail-archive.com/internals@lists.php.net/msg18638.html
[2] http://www.mail-archive.com/internals@lists.php.net/msg42883.html
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 11:01:34 2024 UTC