+1  A: 

Yes. Strings can be seen as Char Arrays, and the way to access a position of an array is to use the [] operator, so there's no problem at all in using $str[0] (and I'm pretty sure is much faster than the substr method.

Have a nice day

Nicolás Hock Isaza

Hock
+3  A: 

My only doubt would be how applicable this technique would be on multi-byte strings, but if that's not a consideration, then I suspect you're covered. (If in doubt, substr seems an obviously safe choice.)

However, from a big picture perspective, I have to wonder how often you need to access the 'n'th character in a string for this to be a key consideration.

middaparka
You're right, it will be an issue with mb strings, thanks for pointing that out. Luckily that won't matter in my case, though. As for your 'big picture', I'm using this to determine the type of product which is identified by the first letter of the product ID.
Tatu Ulmanen
+1  A: 

I've used that notation before as well, with no ill side effects and no misunderstandings. It makes sense -- a string is just an array of characters, after all.

Kaleb Brasee
+1  A: 

Speaking as a mere mortal, I would stick with $str[0]. As far as I'm concerned, it's quicker to grasp the meaning of $str[0] at a glance than substr($str, 0, 1). This probably boils down to a matter of preference.

As far as performance goes, well, profile profile profile. :) Or you could peer into the PHP source code...

Skinniest Man
+6  A: 

The {} syntax is deprecated as of PHP 6. Square brackets are recommended.

Michael Morton
http://docs.php.net/language.types.string : `Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 5.3.0. Use square brackets instead, such as $str[42].`
VolkerK