Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

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 show

Fix it fast

Most common cause, 1 of 3

The 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.

Fix
<?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");
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to PHP 8.2-8.6, behaviour unchanged since PHP 8.0
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
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:

  1. 1The integer was meant as a character codeThe needle is a small literal such as 9 (tab), 10 (newline) or 13 (carriage return), and the call now returns false
  2. 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)
  3. 3An array of words is passed as the needleThe 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 behavior
  • Uncaught TypeError: strpos(): Argument #2 ($needle) must be of type string, int given
  • strpos() 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.

Check
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.

Fix
<?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.

Verify
var_dump(strpos("a\nb", chr(10))); // int(1)

Confirmed by php.net

Did this fix it?

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.

Check
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.

Fix
<?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.

Verify
php -d error_reporting=-1 -d display_errors=1 check-order.php

Confirmed by php.net

Did this fix it?

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.

Check
grep -rn "strpos(" src/ | grep -vE "strpos\([^,]+, *['\"]"

Fix

Loop over the words, or use a regular expression with the words escaped.

Fix
<?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.

Verify
var_dump($hasBlocked);

Confirmed by php.net

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.