[PHP] Check whether a string contains another string

Created on November 30, 2026

strpos finds the first position of the second string inside the first string. It returns false if there is no match. Use !== false so a match at position 0 is also counted.

<?php

$haystack = '123abc456';
$needle = 'abc';

if (strpos($haystack, $needle) !== false) {
    echo 'Substring found';
} else {
    echo 'Substring not found';
}

Output

Substring found
PHP