PHP str_contains() Function
Example
Check if a string contains a specific substring:
<?php
$txt = "I really love PHP!";
var_dump(str_contains($txt, "love")); //
bool(true)
?>
Try it Yourself »
Definition and Usage
The str_contains() function checks if a string contains a specific substring.
Note: This function is case-sensitive.
Note: This function is binary-safe.
Syntax
str_contains(string1, string2)
Parameter Values
| Parameter | Description |
|---|---|
| string1 | Required. Specifies the main string to be searched |
| string2 | Required. Specifies the substring to search for |
Technical Details
| Return Value: | Returns a boolean |
|---|---|
| PHP Version: | 8+ |
More Examples
Example
Showing case-sensitivity:
<?php
$txt = "I really love PHP!";
var_dump(str_contains($txt, "Love")); //
bool(false)
?>
Try it Yourself »
Example
Checking the existence of an empty string will always return true:
<?php
$txt = "PHP";
var_dump(str_contains($txt, "")); //
bool(true)
?>
Try it Yourself »
❮ PHP String Reference