View Full Version : Checking the first character of a string?

03-25-2004, 11:52 PM

I need to retrieve a string from a VARCHAR and before I display it I need to check check if the first character is a <

How can I isolate the first character from a string variable?

Thanks in advance.

03-26-2004, 12:18 AM
if ($row['thevar']{0} == '<'){
echo $row['thevar'] ;

03-26-2004, 12:52 AM
Thanks, I am embarrased to ask such simple things, I do try to search for answers first but I guess I don't search the correct way.

03-26-2004, 08:51 AM
Well, usualy searching he PHP manual at www.php.net delivers a sollution. In this particular case, i don't know if you would have found the above sollution, since it's not such a frequantly used 'thingy'

Most people would use substr($var,0,1) and it's on the substr-page that i recently saw this inside on of the codeexamples.

03-30-2004, 11:01 PM

03-30-2004, 11:17 PM
You could also use the strstr() (http://www.php.net/strstr) function without having to use substr. Of course even if it does not start with < but it has < somewhere in the string, it will be true.
:confused: i fail to see the point of your post. strstr() can not be used for this, for exactly the reason you mention. i also don't understand the "without having to use substr" --> is there something problematic with substr() ?
and if you don't want to use it, then
if ($row['thevar']{0} == '<'){
is still the best approach.
So i don't exactly see your point...