strpos() with a number as needle: the PHP 7.3 notice is gone, the bug is not
Your error
Deprecated: strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior2 more lines it can showFix it fast
Most common cause, 1 of 3The integer was meant as a character code
Turn the code into the character it stood for, with chr() or a double-quoted escape, exactly as the migration guide suggests.
<?php
// before: worked on PHP 7.x, finds the text "10" on PHP 8
$pos = strpos($line, 10);
// after
$pos = strpos($line, chr(10));
$pos = strpos($line, "\n");Short answer
Deprecated: strpos(): Non-string needles will be interpreted as strings in the future came from PHP 7.3 and 7.4. Since PHP 8.0 an int needle is searched as its digits, so strpos($s, 10) looks for "10" rather than a newline, and with strict_types it throws a TypeError. Use chr() for a character code or pass a string. Applies to PHP 8.x, including 8.6.
Which cause do you have?
Find the search calls whose needle is a bare number or a variable, then dump that needle at runtime. A small literal such as 9, 10 or 13 is cause 1. A number variable in a file that declares strict_types is cause 2. An array is cause 3.
grep -rnE "(strpos|stripos|strrpos|strstr|stristr|strrchr)\([^,]+, *[0-9$]" src/
// at the failing call:
var_dump($needle);Then find the line you see in this list and open its fix:
- 1The integer was meant as a character code
The needle is a small literal such as 9 (tab), 10 (newline) or 13 (carriage return), and the call now returns false - 2A numeric value meets declare(strict_types=1)
Uncaught TypeError ... must be of type string, int given, and the file starts with declare(strict_types=1) - 3An array of words is passed as the needle
The TypeError ends in array given, with or without strict_types
What does the error look like?
Deprecated: strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behaviorUncaught TypeError: strpos(): Argument #2 ($needle) must be of type string, int givenstrpos() returns false after the PHP 8 upgrade on input where it used to find a match
Cause 1 of 3
The integer was meant as a character code
You seeThe needle is a small literal such as 9 (tab), 10 (newline) or 13 (carriage return), and the call now returns false
Check
Old code that passed a number to strpos() relied on PHP 7, which read an int needle as an ASCII code point; PHP 7.3 deprecated that with the Non-string needles message. The PHP 8.0 migration guide states that the needle of strpos(), strrpos(), stripos(), strripos(), strstr(), strchr(), strrchr() and stristr() is now always read as a string, so 10 means the two characters 1 and 0.
grep -rnE "str(i|r|ri)?pos\([^,]+, *(9|10|13)\)" src/Fix
Turn the code into the character it stood for, with chr() or a double-quoted escape, exactly as the migration guide suggests.
<?php
// before: worked on PHP 7.x, finds the text "10" on PHP 8
$pos = strpos($line, 10);
// after
$pos = strpos($line, chr(10));
$pos = strpos($line, "\n");Verify
The call finds the newline again on a sample line that contains one.
var_dump(strpos("a\nb", chr(10))); // int(1)Confirmed by php.net
Cause 2 of 3
A numeric value meets declare(strict_types=1)
You seeUncaught TypeError ... must be of type string, int given, and the file starts with declare(strict_types=1)
Check
When the needle is an ID, a year or a count held in an int, a file without strict typing silently converts it to its digits, but a file that declares strict_types=1 accepts only an exact string and throws a TypeError. The PHP manual describes strict mode as per file and applying to the calls made from that file.
grep -rln "declare(strict_types=1)" src/ | xargs grep -nE "strpos\(|stripos\("Fix
Convert the number on purpose, so the code says that its digits are what you search for.
<?php
declare(strict_types=1);
$found = strpos($reference, (string) $orderId) !== false;
// PHP 8: the same test, easier to read
$found = str_contains($reference, (string) $orderId);Verify
The script runs without the TypeError and finds the ID in a reference such as INV-1042.
php -d error_reporting=-1 -d display_errors=1 check-order.phpConfirmed by php.net
Cause 3 of 3
An array of words is passed as the needle
You seeThe TypeError ends in array given, with or without strict_types
Check
strpos() takes one string needle, and an array is not a scalar value that PHP can coerce, so the call fails with a TypeError even in the default coercive mode. The code usually meant to test several words at once.
grep -rn "strpos(" src/ | grep -vE "strpos\([^,]+, *['\"]"Fix
Loop over the words, or use a regular expression with the words escaped.
<?php
$hasBlocked = false;
foreach ($blockedWords as $word) {
if (str_contains($comment, $word)) { $hasBlocked = true; break; }
}
$pattern = '/' . implode('|', array_map(fn ($w) => preg_quote($w, '/'), $blockedWords)) . '/';
$hasBlocked = preg_match($pattern, $comment) === 1;Verify
Both versions return true for a comment that holds one of the words, and no TypeError is thrown.
var_dump($hasBlocked);Confirmed by php.net
How do I stop it coming back?
- Before an upgrade from PHP 7, grep for string search calls with numeric or variable needles and give each an explicit chr() or (string).
- Run static analysis at a level that reports int arguments passed to string parameters, so new cases fail in CI.
- Prefer str_contains() for yes-or-no checks, which avoids the 0-versus-false trap of strpos() as well.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Spotless Laundry, EZ Estimater.