PHP str_contains() Function
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.
- It always returns a boolean value.
- It will return TRUE in case of checking for the substring as empty.
- It is case-sensitive.
- This function is binary-safe.
- 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.
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"