RyuMaster Posted October 26, 2009 Share Posted October 26, 2009 Hi! I have very simple comparing code. The $str includes $compare, and should return "YES", but I get "NO". Why is it so? Am I missing something? <?php $str = "According to the management guru Peter Drucker (1909–2005), the basic task of a .... Branches of management theory also exist relating to nonprofits and to"; $compare = "According to the management guru Peter"; if(strpos($str,$compare)) { echo "YES"; } else { echo "NO"; } ?> Regards! Konstantin. Quote Link to comment https://forums.phpfreaks.com/topic/179082-strpos-not-working-why/ Share on other sites More sharing options...
knsito Posted October 26, 2009 Share Posted October 26, 2009 because the function is returning 0 you should change the if to if(strpos() !== false){...} See the return value section and first example here http://php.net/manual/en/function.strpos.php Quote Link to comment https://forums.phpfreaks.com/topic/179082-strpos-not-working-why/#findComment-944823 Share on other sites More sharing options...
JAY6390 Posted October 26, 2009 Share Posted October 26, 2009 Yup since the match is right at the start (the matching starts at 0) and false equates loosely to 0 they create a false instead of a true. use !== false as suggested above or === true Quote Link to comment https://forums.phpfreaks.com/topic/179082-strpos-not-working-why/#findComment-944874 Share on other sites More sharing options...
knsito Posted October 27, 2009 Share Posted October 27, 2009 Yup since the match is right at the start (the matching starts at 0) and false equates loosely to 0 they create a false instead of a true. use !== false as suggested above or === true I dont think === true will work since the function returns the integer position. Quote Link to comment https://forums.phpfreaks.com/topic/179082-strpos-not-working-why/#findComment-945619 Share on other sites More sharing options...
Alex Posted October 27, 2009 Share Posted October 27, 2009 Yup since the match is right at the start (the matching starts at 0) and false equates loosely to 0 they create a false instead of a true. use !== false as suggested above or === true I dont think === true will work since the function returns the integer position. Correct, === true will not work. Just use !== false or === false. Quote Link to comment https://forums.phpfreaks.com/topic/179082-strpos-not-working-why/#findComment-945623 Share on other sites More sharing options...
severndigital Posted October 27, 2009 Share Posted October 27, 2009 you might want to use stristr() instead of strpos to compare, which is what it looks like you are doing http://us2.php.net/manual/en/function.stristr.php Quote Link to comment https://forums.phpfreaks.com/topic/179082-strpos-not-working-why/#findComment-945624 Share on other sites More sharing options...
MadTechie Posted October 27, 2009 Share Posted October 27, 2009 Correct, === true will not work. true Just use !== false or === false. Huh! Assume that's a typo AlexWD ! Quote Link to comment https://forums.phpfreaks.com/topic/179082-strpos-not-working-why/#findComment-945632 Share on other sites More sharing options...
Alex Posted October 27, 2009 Share Posted October 27, 2009 Typo? I don't think so. Maybe I didn't explain what I meant well enough. I mean like this: <?php $str = 'Blah Blah Blah. Hello World'; if(strpos($str, 'Blah') !== false) // Use !== false to see if it's present { echo 'Found'; } if(strpos($str, 'Not found') === false) // Use === false to see if it's not present { echo 'Not Found'; } Quote Link to comment https://forums.phpfreaks.com/topic/179082-strpos-not-working-why/#findComment-945638 Share on other sites More sharing options...
MadTechie Posted October 27, 2009 Share Posted October 27, 2009 Ahh that makes more sense, I thought you meant if(strpos() === false){...} or if(strpos() !== false){...} of course the above is just silly so assumed, it was a typo. Quote Link to comment https://forums.phpfreaks.com/topic/179082-strpos-not-working-why/#findComment-945776 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.