Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Repair Windows errors before they cause bigger problems3Scan for outdated or missing drivers - takes under a minuteYes. PHP’s strpos() can search for a multi-character substring: pass the substring as $needle. It returns the zero-based position of the first match, or false if there is no match. Use === false to check for failure, because a match at the start of the string returns 0.
How to search for a multi-character substring
The $needle parameter is a string, so it can contain one character or a longer sequence. strpos() finds the first occurrence of that sequence in the haystack.
<?php
$haystack = 'The quick brown fox';
$needle = 'brown';
$position = strpos($haystack, $needle);
if ($position === false) {
echo 'Not found';
} else {
echo "Found at byte position $position";
}
For this example, the function returns 10, the zero-based position where brown begins. The PHP manual documents the signature as strpos(string $haystack, string $needle, int $offset = 0): int|false and describes it as finding the numeric position of the first occurrence of the needle. See the PHP strpos() manual.
Check the result without confusing position 0 for failure
A match at the beginning of the haystack has position 0. Because 0 is false-like in PHP, a loose check such as if (!$position) incorrectly treats that valid match as missing. Compare strictly with false:
#1 Best Overall
if ($position === false) {
// No match
} else {
// Match found, including at position 0
}
The returned position is zero-based. If no occurrence exists, the return value is the boolean false.
Case sensitivity and a boolean alternative
strpos() is case-sensitive: searching for 'Brown' will not match 'brown'. If you only need to know whether a substring occurs, rather than its position, PHP 8 and later provide str_contains(), which returns a boolean and is also case-sensitive.
Rank #2
if (str_contains($haystack, 'brown')) {
echo 'Found';
}
Use strpos() when the position matters; use str_contains() for a yes-or-no containment check. See the PHP str_contains() manual.
Offsets and edge cases
The optional offset controls where the search starts. A positive offset begins at that position. Negative offsets are supported starting with PHP 7.1.0 and search from a point counted back from the end of the string; any returned position is still relative to the beginning of the haystack. An offset greater than the haystack’s length throws a ValueError.
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →In PHP 8 and later, an empty needle is valid: it matches at every position. With no offset, strpos() returns 0; with an offset, it returns that offset. If an empty needle should count as invalid in your application, reject it explicitly before calling the function.
The needle should be a string. Integer needles were deprecated in PHP 7.3.0 and are no longer supported as of PHP 8.0.0. If an integer represents a character ordinal, use chr() explicitly; if you mean its digits as text, cast it to a string.
Quick Recap
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




