Open In App
Related Articles

PHP str_contains() Function

Like Article
Save Article
Report issue

The str_contains() is a predefined function that is introduced with the release of PHP 8. The str_contains() function search for the substring in the given string within the expression. If the substring mentioned will be present in the string then it will return True otherwise it will return False. The str_contains() function is very similar to strpos() function.


  1. It always returns a boolean value.
  2. It will return TRUE in case of checking for the substring as empty.
  3. It is case-sensitive.
  4. This function is binary-safe.
  5. It is only supported on PHP 8 or higher versions.


(str_contains('String', 'Substring')) ;

A substring is a string that needs to be searched whereas a string is a part where a substring is to be searched.

Note: str_contains() is only supported in PHP 8 or higher versions.

Example: In the below example, we have a sentence stored in $sentence and a word stored in $word. In this example, we are trying to check whether the word is present in the sentence or not by using the str_contains() function. If the word will be present in the sentence ‘is’ will be assigned to $result otherwise its value will be ‘is not‘. We will understand this using example.


$sentence = 'GFG is Awesome';
$word = 'GFG';
$result = str_contains($sentence, $word) ? 'is' : 'is not';
echo "The word {$word} {$result} present in the sentence \"{$sentence}\" ";


From the above, we have seen how to find the substring in a given string by using the str_contains() function & the return value will be the boolean. 


The word GFG is present in the sentence "GFG is Awesome"


Last Updated : 09 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads