How would you find out if a string contains another string in PHP?

Suppose we have a string that is stored in a PHP variable called $aString. And we want to find out if inside of $aString there is another substring – let’s just say for the sake of an example that we are looking for the string “Waldo” inside of the larger string.

Now let’s say that the name of the larger string ($aString) is
this: “Where is Waldo?”. And, we just want to find out if $aString contains “Waldo”. PHP provides us with a function called strpos that will allow us to find the existence of one string inside of another. Here is an example of how to use the strpos function:

Example of how to find out if one string contains another in PHP

if (strpos($aString,'Waldo') !== false) {
    echo 'I found Waldo!';

But, there is something you should be aware of when using the strpos function: if you are looking for “Waldo” inside a string that looks like this: “heyWaldo are you there?”, then the strpos function will return successfully with the string positon of “Waldo”, basically saying that “Waldo” was indeed found. This is of course a problem if you only want to search for the string “Waldo” as a separate word, and not as part of another word.

Strpos never returns true

One thing about the strpos function that you should remember is that it never returns the boolean value of true. The strpos function returns a value indicating the position of the first occurrence of the substring being searched for. If the substring is not found “false” is returned instead – which is why in the code above we check for false instead of true.

!== vs != in PHP

One thing worth noting in the code above is that we used the !== operator instead of the != operator (which has one less “=”). What’s the difference between the 2 operators?

You can think of the !== operator as being more ‘strict’ than the != operator. This is because the !== operator will say that the two operands being compared are not equal only if the type of the two operands are the same, but their values are not equal.

This is desirable behavior because the strpos function can return a 0 if the string being searched contains the substring as the very first element. The 0 would represent the 0th index of the larger string – meaning the first position in that string. So, if $aString is “Waldo is here”, and we are searching for “Waldo”, then the strpos function will return a 0. This means that the check being performed will be to see if 0 is not equal to false. But the problem is that 0 is also considered as the integer equivalent of the boolean ‘false’ in PHP, which means that the statement “0 != false” will be considered false, because 0 is equal to false in PHP.

But, if we run “0 !== false” instead, then that statement will be considered to be true, because it just adds the additional check to see if 0 and false are of the same type. Since 0 is an integer and false is a boolean, clearly they are not equal so comparing the 0 and false for inequality returns true unlike the “0 != false” check, which returns false.

Hopefully that was not too confusing and if you need more details on that concept you can read about it here: Difference between == and === in PHP.

if we had this code instead – where we use != and not !== – then it would be a problem:

Problematic code to find a substring inside a larger string

if (strpos($aString,'Waldo') != false) {
    echo 'I found Waldo!';

The code above can result in problems for the reasons discussed above. It’s always better to use !== instead of !=.

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.

Leave a Reply

Your email address will not be published.