views:

322

answers:

4

Is there any equivalent function that returns the character at position X in PHP?

I went through the documentation but couldn't find any. I am looking for something like:

$charAtPosition20 = strCharAt(20, $myString);
A: 

Nevermind found it :)

echo $str[1];

// so obvious

fmsf
The {} syntax is deprecated as of PHP 6. Square brackets are recommended.
Michael Morton
ok ty found this in an example online I'll correct it
fmsf
+1  A: 
echo $myString{20};

or:

substr($mystring, 20, 1)
p00ya
+8  A: 

You can use: $myString[20]

Michael Morton
+1 for the [] brackets
fmsf
+1 for not putting substr()
Tom Haigh
A: 

There are two ways you can achieve this:

a) use the index directly to access the character at the location, like, $string[$index] b) use the substr function: string substr ( string $string , int $start [, int $length ] ) http://us2.php.net/manual/en/function.substr.php

mkamthan