A character from a string

You can get a specific character from a string as if the string was an array.
< ?php
$s = 'PHP';
echo $s[0]; // will print "P"
?>
This syntax may be confusing, because someone looking at the code might expect that $s is an array. Well it is an array, an array of characters, but that's not the point. The point is that if might be confusing.

The same result may be achieved with curly braces.
< ?php
$s = 'PHP';
echo $s{1}; // will print "H"
?>
The numbering is zero-based, the first character in the string has offset 0.

This entry was posted on Tuesday, March 29th, 2005 and is filed under PHP certification. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

Leave a Reply