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
strripos() - Find the position of the last occurrence of a case-insensitive substring in a string ... For earlier versions of PHP, you can polyfill the str_contains function using the following snippet: <?php // based on original work from the PHP Laravel framework if (!function_exists('str_contains')) { function str_contains($haystack, $needle) { return $needle !== '' && mb_strpos($haystack, $needle) !== false; } } ?>
🌐
W3Schools
w3schools.com › php › php_string.asp
PHP Strings
In PHP, strings are surrounded by either double quotes, or single quotes.
Top answer
1 of 16
8099

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

$a = 'How are you?';
$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,$i,$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 ($arr as $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;
    }
🌐
W3Schools
w3schools.com › php › func_string_str_contains.asp
PHP str_contains() Function
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-strings
PHP Strings - GeeksforGeeks
April 11, 2025 - Strings are enclosed in either single quotes (' ') or double quotes (" "). You can create a string using single quotes (' ') or double quotes (" "). PHP supports special syntax like heredoc and nowdoc for multiline strings.
🌐
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 ...
🌐
Droptica
droptica.com › blog › combining-string-literals-and-variables-php
Combining strings and variables in PHP 8+: examples and common mistakes
For deeper runtime context, see how the PHP interpreter works. ... PHP gives you four common ways to build dynamic strings: concatenation with single quotes, interpolation inside double quotes, sprintf() formatting and heredoc/nowdoc blocks.
Find elsewhere
🌐
Atma Ram Sanatan Dharma College
arsdcollege.ac.in › wp-content › uploads › 2020 › 03 › B.Sc_.H4thSem_SEC_PHP-1.pdf pdf
PHP String
PHP string. ... In PHP, we can specify string through enclosing text within double quote also.
🌐
Medium
medium.com › @TechnologyDiaries › php-strings-a-comprehensive-guide-183276a0e2b5
PHP Strings : A Comprehensive Guide | by Technology Diaries | Medium
August 4, 2025 - In PHP, a string is a sequence of characters that can include letters, numbers, symbols, and whitespace.
🌐
W3Schools
w3schools.com › php › php_string_functions.asp
PHP String Functions
Check if a string starts with a specific substring: $txt = "I really love PHP!"; var_dump(str_starts_with($txt, "I really")); Try it Yourself » · Note: This function performs a case-sensitive search. The following example will return a boolean false, because "i really" is not found in the main string:
🌐
CodeSignal
codesignal.com › learn › courses › practicing-string-operations-and-type-conversions-in-php › lessons › string-parsing-and-arithmetic-operations-in-php
String Parsing and Arithmetic Operations in PHP
When we encounter a digit, we append it to our $num string. If a character isn’t a digit and $num isn’t empty, it means we've reached the end of a number. At this point, we convert $num to an integer, add it to the numbers array, and reset $num to an empty string.
🌐
The Man in the Arena
carlalexander.ca › php-string-formatting
PHP strings and how to format them | The Man in the Arena
August 3, 2017 - Now, what happens if you wanted to store Don't hack core! inside a string literal? There’s a single quote inside your value. So you can’t write your string literal as 'Don't hack core!'. PHP would throw an error if you did that.
🌐
Medium
medium.com › @thecodeliner › useful-php-string-functions-4c763684c07e
Useful PHP String Functions
June 3, 2025 - Hey there! If you’re working with PHP, strings are probably a big part of your coding life. Whether you’re building a website, handling user input, or formatting text, PHP has some super handy string functions to make your job easier. In this article, I’ll walk you through some of the most useful ones in simple terms, with examples to show how they work.
🌐
Tutorialspoint
tutorialspoint.com › php › php_strings.htm
PHP - Strings
A sequence of characters enclosed in single quotes (the character ') is a string. ... If you want to include a literal single quote, escape it with a backslash (\). <?php $str = 'This is a \'simple\' string'; echo $str; ?>
🌐
CodeSignal
codesignal.com › learn › courses › interview-practice-with-classic-coding-questions-in-php › lessons › introduction-to-string-manipulation-in-php
Introduction to String Manipulation in PHP
Finding the longest common prefix among an array of strings involves iterating character by character over the strings, starting from the first character. We compare the characters at the same position across all strings until we find a mismatch or reach the end of one of the strings. The common characters encountered up to this point form the longest common prefix. This approach ensures we only retain characters that are common to all strings from the beginning. ... <?php function longestCommonPrefix($strs) { if (count($strs) == 0) return ""; $shortest = $strs[0]; foreach ($strs as $str) { if