PHP str_contains() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP 8.0 and later, str_contains() checks whether a string contains another string.

It returns true or false, so there is no need to compare against position values.

Syntax

Call str_contains() with the values you need. Start with this line.

PHP
$found = str_contains($text, "php");
CallWhat it does
str_contains("Hello PHP", "PHP")Returns true.
str_contains("Hello PHP", "Java")Returns false.

The check is case-sensitive. An empty needle always returns true.

Library declarations
PHP
str_contains(string $haystack, string $needle): bool

Parameters

ParameterDescription
haystackRequired. The string to search in.
needleRequired. The text to look for.

Return Value

Return ValueDescription
booltrue if the needle is found in the haystack, otherwise false.

PHP Examples

Example 1 — Check for a Word

Ask if one string contains another.

PHP
<?php
$text = "Learn PHP with CodeToFun";

if (str_contains($text, "PHP")) {
    echo "Found PHP", PHP_EOL;
} else {
    echo "No PHP", PHP_EOL;
}

How It Works

  1. Pick a text. It has a few words.
  2. Call str_contains(). The haystack comes first.
  3. Get true. PHP is inside the text.
  4. Use it in if. No need for !== false.

Example 2 — Case Matters and Empty Needle

Letter case changes the result, and an empty needle always matches.

PHP
<?php
echo str_contains("Hello", "hello") ? "found" : "not found", PHP_EOL;
echo str_contains("Hello", "") ? "found" : "not found", PHP_EOL;

How It Works

  1. Search with different case. H and h are not the same here.
  2. Get false. The first line prints not found.
  3. Search for an empty string. Every string contains it.
  4. Get true. The second line prints found.

Example 3 — Filter an Array

Use str_contains() inside array_filter().

PHP
<?php
$files = ["app.php", "style.css", "form.php"];

$phpFiles = array_filter($files, fn($file) => str_contains($file, ".php"));

echo implode(", ", $phpFiles), PHP_EOL;

How It Works

  1. Start with a list. It has three file names.
  2. Write a short callback. The arrow function returns true or false.
  3. array_filter() keeps matches. Only names with .php remain.
  4. Print the result. implode() joins them.

Frequently Asked Questions

str_contains() checks if a string contains a given substring and returns true or false.
str_contains(string $haystack, string $needle): bool. The string to search comes first, then the text to find.
It was added in PHP 8.0. On older versions, use strpos() !== false.
Yes. "Hello" does not contain "hello". Lowercase both strings first, or use stripos(), for a case-insensitive check.
It always returns true. Every string is considered to contain an empty string.

Next: str_decrement()

str_decrement() moves an alphanumeric string one step back.

str_decrement() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful