PHP string contains - check if string contains substring

For a PHP string contains check, use str_contains() to test whether one string includes another and get a boolean result. It fits guard clauses, routing checks, and input validation where only a yes/no answer matters. Compared to strpos(), it avoids the 0-is-falsy pitfall and reads cleanly without !== false. The str_contains() is available in PHP 8.0+.

PHP String Contains Example For Substring Check

Output:

Output will appear here...

Output:

Status: shipped

How This Example Works

  1. $message holds a plain string with a status description.
  2. str_contains($message, "shipped") scans the haystack for the exact substring "shipped" and returns true because it appears at position 6.
  3. The if block executes because the return value is already a boolean — no type-juggling comparison needed.

What Is str_contains in PHP?

str_contains(string $haystack, string $needle): bool is a PHP 8.0 function that performs a case-sensitive, binary-safe substring check. It returns true when $needle is found anywhere inside $haystack, and false otherwise. An empty $needle always returns true — a defined behavior in the PHP specification, not a bug.

Common Mistakes With str_contains()

Expecting case-insensitive matching

Wrong:

str_contains("Hello World", "hello"); // false

Right:

stripos("Hello World", "hello") !== false; // true

str_contains() is case-sensitive. For case-insensitive searches, use stripos() !== false (or normalize both strings before calling str_contains()).

Treating strpos() like a boolean (migration pitfall)

Wrong:

if (strpos("shipped", "ship")) echo "match"; // no output (0 is falsy)

Right:

if (strpos("shipped", "ship") !== false) echo "match";

On PHP 8+, prefer str_contains() when you only need a boolean.

str_contains vs strpos vs stripos

FunctionReturnsCasePHP Version
str_contains()boolSensitive8.0+
strpos()int|falseSensitive4+
stripos()int|falseInsensitive5+

Use str_contains() when you only need a boolean check on PHP 8+. Use strpos() when you need the match position or must support PHP 7.x. Use stripos() for case-insensitive containment when you also need the match position (it returns the index like strpos()).

FAQ

Is str_contains case-sensitive?

Yes. str_contains("PHP", "php") returns false. For case-insensitive checks, either lowercase both arguments with strtolower() before calling str_contains(), or use stripos($haystack, $needle) !== false.

Why does str_contains return true for an empty string?

By definition, the empty string is a substring of every string. This matches the behavior of strpos("anything", "") returning 0 in PHP 8. Guard against empty user input before calling str_contains() if that would cause incorrect logic in your code.

How do you check string contains in PHP 7?

Use strpos($haystack, $needle) !== false. The strict !== false comparison is critical because strpos() returns 0 when the needle is at the start — and 0 is falsy in PHP. This pitfall is the main reason str_contains() was introduced.