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?
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)
When I first started PHP I used to prefix a dummy character to the “haystack” until I understood…
Thanks to Dave and John!
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.