Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Basics (Retired) PHP Datatypes Strings

get subset of string in PHP

I am following the new PHP Basics - String & Booleans episode. In the tutorial, Hampton use a method to obtain a letter in a string, like

$name = "Jason";
echo $name{0};

It does not work for me. I am using "PHP 5.4.24 (cli)" on a Mac machine. Anyone has some idea what may goes wrong. Thanks.

3 Answers

Chris Malcolm
Chris Malcolm
2,909 Points

try [] not {}

<?php
echo $name[0];
?>
Oğulcan Girginc
Oğulcan Girginc
24,848 Points

If that's the case, how come { } works in Hampton Paulk's example? :/

Josh Grupp
Josh Grupp
7,060 Points

Chris has this right, the square brackets "[" and "]" are what you use for specifying the position in the string.