How to check if a string contains a substring in PHP?

To check if the PHP string contains a substring, you can use the strpos() function. If the string contains the substring, strpos() will return the position (index) of the first occurrence of the substring in the string and "false" otherwise. In PHP, indexes start at 0. As of PHP 8+, you can use the new str_contains() function to check if a string contains the desired substring or word. The str_contains() function returns "true" if the string contains a substring or "false" otherwise. In this PHP String Contains Substring example, we use the strpos() function to check if the string contains a substring. Click Execute to run the PHP example online and see the result.
Loading...