Now with PHP 8 you can do this using str_contains:

if (str_contains('How are you', 'are')) { 
    echo 'true';
}

Please note: The str_contains function will always return true if the $needle (the substring to search for in your string) is empty.

$haystack = 'Hello';
$needle   = '';

if (str_contains($haystack, $needle)) {
    echo "This returned true!";
}

You should first make sure the $needle (your substring) is not empty.

$haystack = 'How are you?';
$needle   = '';

if ($needle !== '' && str_contains($haystack, $needle)) {
    echo "This returned true!";
} else {
    echo "This returned false!";
}

Output: This returned false!

It's also worth noting that the new str_contains function is case-sensitive.

$haystack = 'How are you?';
$needle   = 'how';

if ($needle !== '' && str_contains($haystack, $needle)) {
    echo "This returned true!";
} else {
    echo "This returned false!";
}

Output: This returned false!

RFC

Before PHP 8

You can use the strpos() function which is used to find the occurrence of one string inside another one:

$haystack = 'How are you?';
$needle   = 'are';

if (strpos($haystack, $needle) !== false) {
    echo 'true';
}

Note that the use of !== false is deliberate (neither != false nor === true will return the desired result); strpos() returns either the offset at which the needle string begins in the haystack string, or the boolean false if the needle isn't found. Since 0 is a valid offset and 0 is "falsey", we can't use simpler constructs like !strpos($a, 'are').

🌐
PHP
php.net › manual › en › function.str-contains.php
PHP: str_contains - Manual
Performs a case-sensitive check indicating if needle is contained in haystack. ... The string to search in. ... The substring to search for in the haystack. Returns true if needle is in haystack, false otherwise. ... <?php if (str_contains('abc', '')) { echo "Checking the existence of the empty string will always return true"; } ?>
🌐
W3Schools
w3schools.com › php › func_string_str_contains.asp
PHP str_contains() Function
The str_contains() function checks if a string contains a specific substring. Note: This function is case-sensitive. Note: This function is binary-safe. ... <?php $txt = "I really love PHP!"; var_dump(str_contains($txt, "Love")); // bool(false) ...
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-str_contains-function
PHP str_contains() Function - GeeksforGeeks
September 9, 2021 - The str_contains() is a predefined function that is introduced with the release of PHP 8. The str_contains() function search for the substring in the given string within the expression.
Top answer
1 of 16
8101

Now with PHP 8 you can do this using str_contains:

if (str_contains('How are you', 'are')) { 
    echo 'true';
}

Please note: The str_contains function will always return true if the $needle (the substring to search for in your string) is empty.

$haystack = 'Hello';
$needle   = '';

if (str_contains($haystack, $needle)) {
    echo "This returned true!";
}

You should first make sure the $needle (your substring) is not empty.

$haystack = 'How are you?';
$needle   = '';

if ($needle !== '' && str_contains($haystack, $needle)) {
    echo "This returned true!";
} else {
    echo "This returned false!";
}

Output: This returned false!

It's also worth noting that the new str_contains function is case-sensitive.

$haystack = 'How are you?';
$needle   = 'how';

if ($needle !== '' && str_contains($haystack, $needle)) {
    echo "This returned true!";
} else {
    echo "This returned false!";
}

Output: This returned false!

RFC

Before PHP 8

You can use the strpos() function which is used to find the occurrence of one string inside another one:

$haystack = 'How are you?';
$needle   = 'are';

if (strpos($haystack, $needle) !== false) {
    echo 'true';
}

Note that the use of !== false is deliberate (neither != false nor === true will return the desired result); strpos() returns either the offset at which the needle string begins in the haystack string, or the boolean false if the needle isn't found. Since 0 is a valid offset and 0 is "falsey", we can't use simpler constructs like !strpos($a, 'are').

2 of 16
760

You could use regular expressions as it's better for word matching compared to strpos, as mentioned by other users. A strpos check for are will also return true for strings such as: fare, care, stare, etc. These unintended matches can simply be avoided in regular expression by using word boundaries.

A simple match for are could look something like this:

$a = 'How are you?';

if (preg_match('/\bare\b/', $a)) {
    echo 'true';
}

On the performance side, strpos is about three times faster. When I did one million compares at once, it took preg_match 1.5 seconds to finish and for strpos it took 0.5 seconds.

Edit: In order to search any part of the string, not just word by word, I would recommend using a regular expression like

search = 'are y';
if(preg_match("/{$search}/i", $a)) {
    echo 'true';
}

The i at the end of regular expression changes regular expression to be case-insensitive, if you do not want that, you can leave it out.

Now, this can be quite problematic in some cases as the $search string isn't sanitized in any way, I mean, it might not pass the check in some cases as if $search is a user input they can add some string that might behave like some different regular expression...

Also, here's a great tool for testing and seeing explanations of various regular expressions Regex101

To combine both sets of functionality into a single multi-purpose function (including with selectable case sensitivity), you could use something like this:

function FindString($needle,$haystack,word)
{   // $i should be "" or "i" for case insensitive
    if (strtoupper($word)=="W")
    {   // if $word is "W" then word search instead of string in string search.
        if (preg_match("/\b{$needle}\b/{$i}", $haystack)) 
        {
            return true;
        }
    }
    else
    {
        if(preg_match("/{$needle}/{$i}", $haystack)) 
        {
            return true;
        }
    }
    return false;
    // Put quotes around true and false above to return them as strings instead of as bools/ints.
}

One more thing to take in mind, is that \b will not work in different languages other than english.

The explanation for this and the solution is taken from here:

\b represents the beginning or end of a word (Word Boundary). This regex would match apple in an apple pie, but wouldn’t match apple in pineapple, applecarts or bakeapples.

How about “café”? How can we extract the word “café” in regex? Actually, \bcafé\b wouldn’t work. Why? Because “café” contains non-ASCII character: é. \b can’t be simply used with Unicode such as समुद्र, 감사, месяц and .

When you want to extract Unicode characters, you should directly define characters which represent word boundaries.

The answer: (?<=[\s,.:;"']|^)UNICODE_WORD(?=[\s,.:;"']|$)

So in order to use the answer in PHP, you can use this function:

function contains($str, array $arr) {
    // Works in Hebrew and any other unicode characters
    // Thanks https://medium.com/@shiba1014/regex-word-boundaries-with-unicode-207794f6e7ed
    // Thanks https://www.phpliveregex.com/
    if (preg_match('/(?<=[\s,.:;"\']|^)' . $word . '(?=[\s,.:;"\']|str)) return true;
}

And if you want to search for array of words, you can use this:

function arrayContainsWord($str, array $arr)
{
    foreach (word) {
        // Works in Hebrew and any other unicode characters
        // Thanks https://medium.com/@shiba1014/regex-word-boundaries-with-unicode-207794f6e7ed
        // Thanks https://www.phpliveregex.com/
        if (preg_match('/(?<=[\s,.:;"\']|^)' . $word . '(?=[\s,.:;"\']|str)) return true;
    }
    return false;
}

As of PHP 8.0.0 you can now use str_contains

<?php
    if (str_contains('abc', '')) {
        echo "Checking the existence of the empty string will always"
        return true;
    }
🌐
W3Docs
w3docs.com › php
PHP string "contains" | W3Docs
This will also output The string contains the word fox. Note that strstr() is generally slower than strpos() because it returns the entire remaining string rather than just a position. For modern PHP (8.0+), str_contains($string, 'fox') is the recommended, most readable approach.
🌐
ReqBin
reqbin.com › code › php › menoknaq › php-string-contains-example
How to check if a string contains a substring in PHP?
In PHP, indexes start at 0. As of PHP 8+, you can use the new str_contains($string, $substring) function to check if a string contains the desired substring or word. The str_contains() function returns "true" if the string contains a substring ...
Find elsewhere
🌐
PHP.Watch
php.watch › versions › 8.0 › str_contains
New `str_contains` function - PHP 8.0 • PHP.Watch
As the name suggests, it checks if the given haystack string contains a given string needle.
🌐
Dev Lateral
devlateral.com › home › guides › php › php string contains substring guide
PHP String Contains Substring Guide | Dev Lateral
November 6, 2023 - The str_contains function in PHP 8 is a great welcomed function that easily solves a frequent task by developers in their code. This function can be used to determine if a string is contained in another given string (substring).
🌐
Reddit
reddit.com › r/php › php code golf: detect if string contains an element from array.
r/PHP on Reddit: PHP Code Golf: Detect if string contains an element from array.
November 17, 2017 - <?php $array = ['quick', 'uick brown fo']; $string = 'the quick brown fox jumps over the lazy dog'; if(YOUR CODE HERE){ // The string contains an element in the array }
🌐
DEV Community
dev.to › ankitvermaonline › how-to-check-if-a-string-contains-specific-words-in-php-1ho
How to check if a string contains specific words in PHP - DEV Community
December 20, 2025 - Check if a string contains a specific word: Use the PHP str_contains() function to determine if a string contains a specific word.
🌐
W3Schools
w3schools.com › php › php_string_functions.asp
PHP String Functions
$txt = "I really love PHP!"; var_dump(str_contains($txt, "love")); Try it Yourself »
🌐
Honar Systems
honarsystems.com › home › php string and substring contains
PHP String And Substring Contains
July 23, 2025 - Sometimes you need to check if a string(str) contains a substring in PHP. In this tutorial, we will discuss str_contains and the other 4.
🌐
Codecademy
codecademy.com › docs › php › string functions › str_contains()
PHP | String Functions | str_contains() | Codecademy
July 1, 2023 - The str_contains() function performs a case sensitive search for a given substring within a string.
🌐
PHP.Watch
php.watch › codex › str_contains
str_contains Function • PHP.Watch
Determine if a string contains a given substring. ... The string to search in. ... The substring to search for in the $haystack. ... Returns true if $needle is in $haystack, false otherwise. ... PHP Codex data is built by collecting PHP symbol data on the latest release of each PHP version, or the nightly builds of the active development branch, and analyzing them to determine their availability and signature data.
🌐
Beamtic
beamtic.com › check-if-string-contains-php
Check if string contains a substring with PHP | Beamtic
July 27, 2020 - if (str_contains('string contains PHP', 'PHP')) { echo 'PHP was found in string'; }
🌐
Nabilhassen
nabilhassen.com › search-for-a-string-inside-another-string-in-php
PHP: Check if a string contains a substring
November 13, 2025 - Each method is concise, accurate, and based on current PHP best practices. str_contains() is the simplest and most readable way to check if string contains substring or check if string contains word.
🌐
SitePoint
sitepoint.com › php
Check whether string contains numbers - PHP - SitePoint Forums | Web Development & Design Community
February 28, 2010 - // TRUE if $subject contains a decimal digit strpbrk($subject, '1234567890') !== FALSE ... Does no-one like just casting to bool?.. ponders ... Does no-one like just casting to bool?.. ponders · I don’t, I prefer to be a little more explicit. Given php’s loose typing, or rather the handling ...
🌐
GitHub
gist.github.com › 4ba76802cb32a0959a920c8fdcf97e58
PHP if string contains · GitHub
PHP if string contains · Raw · str_contains.php · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.