Check if a string contains a substring in PHP

Today we’ll explain to you how to check if a string contains a substring in PHP. Using PHP in-built strpos() function, we can easily check whether a string contains substring or not.

The strpos() function returns the position of the first appearance of a substring in a string. If the substring is not found then it returns the false.

Note: The strpos() function check string positions start at 0, and not 1.



  • $string: This parameter specifies the original string in which we need to find the substring.
  • $substring: It specifies the substring which we need to search.

Let’s take examples for better understanding.

Example 1

The following code returns the true value because the main string contains the substring.

Example 2

In another example, we will check if a string contains a substring at the beginning.

Example 3

The strpos() function is case-sensitive. So, you can use the stripos() function which is case-insensitive which checks the uppercase and lowercase equally.

That’s it for today.
Thank you for reading. Happy Coding..!!

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *