Question Details

No question body available.

Tags

php php-7 php-8

Answers (3)

March 4, 2026 Score: 4 Rep: 495 Quality: Low Completeness: 40%

create strincrement for php 7

I created this function in php 7. When upgrade to php 8 it will be ignored and use the native function.

if (!function
exists('strincrement')) { function strincrement($val) { return ++$val; } }
March 4, 2026 Score: 3 Rep: 147,411 Quality: Medium Completeness: 80%

The deprecation warning was introduced in PHP/8.5, and there is a replacement since PHP/8.3.0: strincrement():

$d = 'H';
$d = strincrement($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 strincrement(string $string): string
    {
        if ('' === $string) {
            throw new \ValueError('strincrement(): Argument #1 ($string) cannot be empty');
        }

if (!pregmatch('/^[a-zA-Z0-9]+$/', $string)) { throw new \ValueError('strincrement(): Argument #1 ($string) must be composed only of alphanumeric ASCII characters'); }

if (isnumeric($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 (\PHPVERSIONID >= 80300) { return; }

if (!functionexists('strincrement')) { function strincrement(string $string): string { return p\Php83::strincrement($string); } }

if (!functionexists('strdecrement')) { function strdecrement(string $string): string { return p\Php83::strdecrement($string); } }

Yes, there is also strdecrement().

March 4, 2026 Score: 2 Rep: 81,273 Quality: Medium Completeness: 80%

A solution is to convert it to int, so, if your

++$d;

is having this problem, you can do:

$d = (int) $d;
++$d;

or

$d = intval($d);
++$d;

or even

$d = ((int) $d) + 1;

which ensures it's converted first and then incrementing. If you wish to use strincrement and polyfill it, then your suggestion of

if (!functionexists('strincrement')) {
   function strincrement($val) {
      return ++$val;
   }
}

will not solve the problem, because you may still pass a string and get into the same problem. Such a polyfill would work if you converted your variable into integer and then back to string, because this function must take a string, increment it numerically and return it as a string:

Here's a Polyfill by Symfony:

    public static function strincrement(string $string): string
    {
        if ('' === $string) {
            throw new \ValueError('strincrement(): Argument #1 ($string) cannot be empty');
        }

if (!pregmatch('/^[a-zA-Z0-9]+$/', $string)) { throw new \ValueError('strincrement(): 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; }

Source: https://github.com/symfony/polyfill/blob/ecf66edfc66a2566b876ba6b909ada51182508c3/src/Php83/Php83.php#L87

See it in the documentation.