substr - why won't this work?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

substr - why won't this work?

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: substr - why won't this work?

Post 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.
Post Reply