Content · Programming · PHP

PHP str_contains() Function: Usage and Examples

Learn str_contains() with clear PHP examples, output, practical scenarios, important behaviors, and common beginner mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

PHP developers sometimes need to check whether a string contains a given substring. This comes up when preparing data, displaying results, or integrating information with other services.

The str_contains() operation handles this particular task. The following examples show its result, its limitations, and a practical way to apply it.

What Is str_contains() in PHP?

Check whether a string contains a given substring.

Its syntax is:

str_contains(string $haystack, string $needle): bool

The syntax shows the expected parameters and return type. Arguments marked with an ampersand are passed by reference and may be modified. Optional parameters control additional behavior; values must match the declared types.

Important: The search is case-sensitive; an empty needle always matches. It is byte-oriented.

Parameters:

  • $haystack — A named input or configuration argument; use the declared type and optional default shown above.
  • $needle — A named input or configuration argument; use the declared type and optional default shown above.

Example 1: Basic Usage

This first example uses a small input to demonstrate the main result of str_contains().

<?php

var_dump(str_contains("Hello PHP", "PHP"));

Output:

bool(true)

The output demonstrates the primary operation: check whether a string contains a given substring.

Example 2: Understanding Another Case

This second example changes the input or option to reveal a different behavior of str_contains().

<?php

var_dump(str_contains("Hello PHP", "php"));

Output:

bool(false)

Compared with the first example, this one illustrates the same operation with a different input shape or option. The search is case-sensitive; an empty needle always matches. It is byte-oriented.

Practical Example: A Real Application Scenario

Here is a practical use of str_contains() with realistic sample information. Adapt the values to match your application.

<?php

$filename = "report-final.csv"; if (str_contains($filename, "final")) { echo "Final version"; }

Output:

Final version

This is a useful starting point, but production code should check errors and validate values before performing the operation. The search is case-sensitive; an empty needle always matches. It is byte-oriented.

Important Behavior and Edge Cases

The search is case-sensitive; an empty needle always matches. It is byte-oriented.

  • Input: Use the expected PHP types and validate user-provided values.
  • Result: Check the return value separately from any modifications to the input.
  • Environment: For extension, database, or filesystem examples, verify availability and handle failures before accessing results.

Comparison With Related Functions

Function or ApproachWhen to Use It
str_contains()Check whether a string contains a given substring.
Related functionsstrpos() returns the numeric position or false; stripos() searches case-insensitively.

Common Beginner Mistakes

MistakeCorrect Approach
Assuming str_contains() behaves like every similar functionThe search is case-sensitive; an empty needle always matches. It is byte-oriented.
Choosing a related operation without checking differencesstrpos() returns the numeric position or false; stripos() searches case-insensitively.
Ignoring prerequisites or return valuesVerify extensions and data sources, and handle unexpected results explicitly.

Key Takeaways

  • str_contains() lets you check whether a string contains a given substring.
  • The search is case-sensitive; an empty needle always matches. It is byte-oriented.
  • strpos() returns the numeric position or false; stripos() searches case-insensitively.
  • Check input, return values, and environmental requirements when adapting the examples.

str_contains() provides a focused PHP operation for check whether a string contains a given substring. Start with the simple examples, then adapt the practical example with the correct checks for your project.

Share this resourceSend this page to someone who may find it useful.