Strpos() problem

Here’s my code:

$haystack = “,18,19,42,”;
$needle = “,18,”;
if (strpos($haystack,$needle)) {
echo ‘TRUE’;
} else {
echo ‘FALSE’;
}

I expect TRUE , but get FALSE.

What am I missing?

That’s because strpos doesn’t return true. You either get FALSE if the string isn’t found, or you get the position that the value begins with (in your case, you’ll get 0)

3 Likes

When I first started PHP I used to prefix a dummy character to the “haystack” until I understood…

https://secure.php.net/manual/en/function.strpos.php

1 Like

Thanks to Dave and John!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.