check exist string in string php 1

check exist string in string php

$myString = 'Hello Bob how are you?';
if (strpos($myString, 'Bob') !== false) {
    echo "My string contains Bob";

Here is what the above code is Doing:
1. We create a string called $myString.
2. We use strpos() to check if the string contains the word “Bob”.
3. If the string contains “Bob”, strpos() will return the position of the first occurrence of “Bob”.
4. If the string does not contain “Bob”, strpos() will return false.
5. We use the !== operator to check if strpos() returns false.
6. If strpos() returns false, we print “My string does not contain Bob”.
7. If strpos() does not return false, we print “My string contains Bob”.

Similar Posts