The str contains PHP is a function which use to return a position of the occurrence of the first substring into a string.
In this article, we will talk about PHP str_contains function in a detailed explanation as well as example programs that could be helpful to your future projects. This topic is a continuation of the previous topic, entitled PHP __call Magic Method.
What is str contains in PHP?
In PHP, str contains is a function that is used to check whether a given string contains a substring. This function can check a string and will return a boolean result once the string contains a substring the result will be true and false otherwise.
Further, this is a new function which has been introduced in PHP 8 version.
Syntax
str_contains(string, substring)
Parameters
Parameters | Description |
string | This string parameter is required and it was intended to search an original string. |
substring | This parameter is required and it was used to scan the string if they have been containing a substring. |
An example program of PHP str contains
<?php $string = "ITSOURCECODE is one of the best website for providing free source code and tutorials" if (str_contains($string, 'best')) { echo "word found!"; } else { echo "wod not found!"; } ?>
Output
word found!
How do I check if a string contains a string in PHP?
In order to check a string if contains a string we need to use the function name PHP str_contains this is a function which has been introduced in the newest version of PHP 8.
The function is simply used for checking a string if it contained a substring it will return a boolean a true and a false.
An example program of PHP str contains
<?php $givenString = 'ITSOURCECODE is one of the best website for providing free source code and tutorials'; // else echo and false echo if (str_contains($givenString, 'best')) { echo "The given string 'best' was found in the string"."\n"; } if (str_contains($givenString, 'Best')) { echo 'The string "Best" was found in the string'; } else { echo '"Lazy" was not found because the case does not match'; } ?>
Output
The string 'best' was found in the string "Lazy" was not found because the case does not match
How do I find a word in a string in PHP?
The strstr() is a built-in function which is similar to strpos function that is used to find a word in a string. It simply takes a position of the first occurrence of a substring to be searched and then the chunk of text which will be searched for.
How do I check if a string contains text?
The includes() method allows us to check if a string contains a text this method returns a boolean a true or false. Once the string contains a text it will return true otherwise it will return false. Thus, this method is case sensitive and case insensitive.
How do you see if a string contains a character?
The contains() method can be used to see if a string contains a specific word or a character they check whether a string contains a sequence of characters and return a boolean a true and a false.
Which types of strings PHP contain?
- heredoc syntax
- nowdoc syntax
- single quoted
- double quoted
Related Articles
- PHP String (with Program Examples )
- PHP Convert to String with Example
- PHP Substring Function With Examples
- How PHP Multiline String With Examples
- How To Find The Length Of A String In PHP With Example
Summary
In summary, you have learned about str contains PHP function. This article also discussed what str contains in PHP, how do I check if a string contains a string, how do I find a word in a string, how do I check if a string contains text, how do you see if a string contains a character, and which types of strings PHP contain.
I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials which could help you a lot.