Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior

Description

Until PHP 8.0, strpos() accepted integers as second argument. Then, it would automagically, transform them into their equivalent. Hence, it returned 2 in PHP 7.4.

In PHP 8.0 and later, the integer is used as a string. Unless the ASCII characters and the string matches, it would return nothing.

Example

<?php

// 99 is ASCII for c
print strpos('abc99', 99);

// displays 3 in PHP 8.0+
// displays 2 in PHP 7.4-

?>

Alternatives

  • Use chr(99) instead of 99, to ensure the second argument is a string.
  • Convert the second argument into a string first.