A practical guide to checking if a string contains a substring in PHP. For PHP 8.0+, str_contains() is the recommended approach as it returns a plain boolean with no edge cases. For older PHP versions, strpos() with strict comparison (!== false) is the safe fallback. The guide covers case-sensitive vs case-insensitive checks (using stripos()), common pitfalls like strpos() returning 0 for matches at position 0, and the edge case where an empty needle matches every string.
Table of contents
IntroductionThe best way to check if a string contains something in PHPstr_contains() vs strpos()Case-sensitive vs case-insensitive checksPractical examplesImportant edge casesWhat if you need to split or replace instead?Conclusion3 Impressions