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 Approach | When to Use It |
|---|---|
str_contains() | Check whether a string contains a given substring. |
| Related functions | strpos() returns the numeric position or false; stripos() searches case-insensitively. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Assuming str_contains() behaves like every similar function | The search is case-sensitive; an empty needle always matches. It is byte-oriented. |
| Choosing a related operation without checking differences | strpos() returns the numeric position or false; stripos() searches case-insensitively. |
| Ignoring prerequisites or return values | Verify 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.