ARTICLE AD BOX
The deprecation warning was introduced in PHP/8.5, and there is a replacement since PHP/8.3.0: str_increment():
$d = 'H'; $d = str_increment($d);If you need to write code that also needs to work in pre-8.3 versions, you can either write a custom wrapper function that makes PHP version detection or, much better, write a pollyfill. You have some in other answers.
For completeness, Symfony provides one that you can use or get inspiration from: symfony/polyfill-php83.
To use it as is, just load the package with Composer. Nothing else is needed.
The relevant source code can be found at https://github.com/symfony/polyfill-php83/blob/v1.33.0/Php83.php#L87-L125:
public static function str_increment(string $string): string { if ('' === $string) { throw new \ValueError('str_increment(): Argument #1 ($string) cannot be empty'); } if (!preg_match('/^[a-zA-Z0-9]+$/', $string)) { throw new \ValueError('str_increment(): Argument #1 ($string) must be composed only of alphanumeric ASCII characters'); } if (is_numeric($string)) { $offset = stripos($string, 'e'); if (false !== $offset) { $char = $string[$offset]; ++$char; $string[$offset] = $char; ++$string; switch ($string[$offset]) { case 'f': $string[$offset] = 'e'; break; case 'F': $string[$offset] = 'E'; break; case 'g': $string[$offset] = 'f'; break; case 'G': $string[$offset] = 'F'; break; } return $string; } } return ++$string; }... and is loaded like this:
use Symfony\Polyfill\Php83 as p; if (\PHP_VERSION_ID >= 80300) { return; } if (!function_exists('str_increment')) { function str_increment(string $string): string { return p\Php83::str_increment($string); } } if (!function_exists('str_decrement')) { function str_decrement(string $string): string { return p\Php83::str_decrement($string); } }Yes, there is also str_decrement().
