How to check if a string does not contain a specific phrase?
php
Solution
The code here:
if (strpos($a, 'are') !== false) {
// The word WAS found
}
Means that the word WAS found in the string. If you remove the NOT (!) operator, you have reversed the condition.
if (strpos($a, 'are') === false) {
// The word was NOT found
}
the === is very important, because strpos will return 0 if the word 'are' is at the very beginning of the string, and since 0 loosely equals FALSE, you would be frustrated trying to find out what was wrong. The === operator makes it check very literally if the result was a boolean false and not a 0.
As an example,
if (!strpos($a, 'are')) {
// String Not Found
}
This code will say the string 'are' is not found, if $a = "are you coming over tonight?", because the position of 'are' is 0, the beginning of the string. This is why using the === false check is so important.
Problem
How to invert the function of How do I check if a string contains a specific word in PHP? ``` if (strpos($a,'are') !== false) { echo 'true'; } ``` So it echoes `true` if `are` is not found in `$a`.