Page 1 of 1

substr - why won't this work?

Posted: Tue May 11, 2010 4:49 am
by simonmlewis
$row->title is "BIGDOG" for this example.

Code: Select all

			$word = substr("$row->title", -4);
			if ($word== "DOG")
			
			{ echo "this should show here";}
For some reason I have to state -4 to get three characters.
It does render DOG if I echo $word, but it will NOT produce the "this should show here" text.

Anyone know why?

Re: substr - why won't this work?

Posted: Tue May 11, 2010 6:00 am
by califdon
Check the manual for how substr() works in PHP: http://php.net/manual/en/function.substr.php. The manual is rarely wrong. You need to use -3, not -4. Despite what you say, the manual says it will return the last 4 characters if you use -4, so then your conditional wouldn't be true and of course your message wouldn't be echoed.