String Containment Checks in PHP
The syntax $a contains 'are' is valid in languages like Swift or Kotlin, but it will throw a parse error immediately if you run it in PHP. In PHP, operators are strictly defined by symbols rather than English keywords for logic. To check if a string contains a specific word, you must use the strpos() function combined with a type cast.
The core logic relies on strpos(string $haystack, string $needle), which returns the integer position of the first occurrence of $needle in $haystack. If the substring is not found, it returns -1. Because this return value can be 0 (indicating the word was found at the very start of the string), a standard equality check against false fails. You must use the loose comparison operator (==) instead of the strict identity operator (===).
Here is the correct implementation:
$a = 'How are you?';
if (($a) !== '' && strpos($a, 'are') !== false) {
echo 'true';
}
The loose comparison !== ensures that when strpos returns 0, the condition evaluates to true. If it returns -1, the condition evaluates to false. However, relying solely on this function has limitations regarding word boundaries. The code above checks if the character sequence "are" exists anywhere in the string. This means "care" or "fear" would also trigger a match if you are looking for the standalone word "are".
Handling Word Boundaries
If your requirement is strictly to find whole words and not substrings within other words, you need to adjust your approach. A simple regex pattern is often the most robust solution for this specific constraint. Regular expressions allow you to define anchors around the target word.
Using preg_match with a negative lookbehind and lookahead ensures that the characters immediately preceding and following the word are not alphanumeric or underscores. This prevents false positives from words like "care" or "fear".
$a = 'How are you?';
$word = 'are';
if (preg_match('/\b' . preg_quote($word, '/') . '\b/i', $a)) {
echo 'true';
}
The \b token represents a word boundary. The preg_quote function is essential here because if the target word contains special regex characters like dots or asterisks, they need to be escaped before being used in the pattern. The i flag makes the search case-insensitive, which is usually desired when checking for whole words unless you specifically need case sensitivity.
Performance Considerations
Choosing between strpos and preg_match often comes down to performance versus precision. strpos is implemented at a lower level in the C runtime of PHP and executes significantly faster than regular expressions. If you are processing millions of lines of text and only care about substring presence, strpos is the preferred choice.
However, if your application logic requires strict word matching to avoid security issues or data integrity problems (such as filtering input tags that might contain "script" inside a larger word), the slight overhead of regex is a necessary trade-off for correctness. In high-throughput scenarios where performance is critical, you might also consider using the mb_strpos function if your strings contain multi-byte characters like emojis or accented letters. Standard strpos counts bytes rather than characters in UTF-8 strings, which can lead to incorrect positions or missed matches depending on how you handle the resulting offset.
Conclusion
There is no single operator for string containment in PHP because the language prioritizes explicit control over implicit convenience. Whether you use strpos for speed and substring checks or preg_match for strict word boundaries depends entirely on your specific data requirements. Always remember to check against false, not 0, when using strpos.
