String dereferencing allows you direct access to individual characters. The dereferencing syntax is just like accessing an array element by it’s index:
$string = 'This is my awesome string!'; echo 'First Character: ' . $string . "\n"; echo 'Tenth Character: ' . $string . "\n"; echo 'Last Character: ' . $string[strlen($string) - 1] . "\n";
Remember that the index starts at 0 just like an array would.
If you are running PHP 5.5+ you have the added benefit of being able to dereference string literals:
echo 'First Character: ' . 'This is my awesome string!';
Personally, I don’t really see any situation where I’d do this, but I guess someone thought it was a good thing to add support for. Hopefully in the coming PHP releases support for specifying ranges like you can in Python will be added (e.g.