PHP str_increment() Function

What is PHP str_increment() Function?

If you want to increment an alphanumeric ASCII string by one, use PHP str_incremenet() function. It works the same way as PHP’s auto-increment ++ (Perl-style increment) works on a string. The function can only increment alphabets (a-z and A-Z) and numbers (0-9).

How PHP str_increment() Function Works

The function increases the rightmost character to its next one. So,

  1. For small letters (a-z)-
    • ‘a’ becomes ‘b’. It is applicable for all a-y characters. Check example 1.
    • But for ‘z’-
      • The function resets the alphabet to ‘a’ and
      • Moves to the left character and
      • Increment it. If it is also ‘z’, the function repeats from step ‘i’. Check example 2.
  2. For capital letters (A-Z)-
    • The same process happens as the capital letters above. Check example 3 & example 4.
  3. For numbers (0-9)-
    • ‘0’ becomes ‘1’. It is applicable for all 0-8 characters. Check example 5.
    • But for ‘9’-
      • The function resets digit to ‘0’ and
      • Moves to the left digit and
      • Increment it. If it is also ‘9’, the function repeats from step ‘i’. Check 6.
  4. For mixed type-
    • The function follows the same rules. Check example 7 & example 8.

Syntax:

str_increment(string)

Parameters:

The Function has 1 required parameter and 1 optional parameter-

string (Required): It specifies a string that the function increments.

Return Values:

The function returns-

  • the incremented string if the input is an alphanumeric string.
  • a ValueError if the input is empty.
  • a ValueError if the input is not empty and not an alphanumeric string.

Examples:

Example 1:

<?php
  $string = "abc";
  echo "The original string is: " . $string . nl2br("\n");
  echo "After incrementing with str_increment() function, the string becomes: " . str_increment($string); 
?>

Output:

The original string is: abc
After incrementing with str_increment() function, the string becomes: abd

Example 2:

<?php
  $string = "abz";
  echo "The original string is: " . $string . nl2br("\n");
  echo "After incrementing with str_increment() function, the string becomes: " . str_increment($string); 
?>

Output:

The original string is: abz
After incrementing with str_increment() function, the string becomes: aca

Example 3:

<?php
  $string = "ABC";
  echo "The original string is: " . $string . nl2br("\n");
  echo "After incrementing with str_increment() function, the string becomes: " . str_increment($string); 
?>

Output:

The original string is: ABC
After incrementing with str_increment() function, the string becomes: ABD

Example 4:

<?php
  $string = "ABZ";
  echo "The original string is: " . $string . nl2br("\n");
  echo "After incrementing with str_increment() function, the string becomes: " . str_increment($string); 
?>

Output:

The original string is: ABZ
After incrementing with str_increment() function, the string becomes: ACA

Example 5:

<?php
  $string = 123;
  echo "The original string is: " . $string . nl2br("\n");
  echo "After incrementing with str_increment() function, the string becomes: " . str_increment($string); 
?>

Output:

The original string is: 123
After incrementing with str_increment() function, the string becomes: 124

Example 6:

<?php
  $string = 129;
  echo "The original string is: " . $string . nl2br("\n");
  echo "After incrementing with str_increment() function, the string becomes: " . str_increment($string); 
?>

Output:

The original string is: 129
After incrementing with str_increment() function, the string becomes: 130

Example 7:

<?php
  $string = "a9";
  echo "The original string is: " . $string . nl2br("\n");
  echo "After incrementing with str_increment() function, the string becomes: " . str_increment($string); 
?>

Output:

The original string is: a9
After incrementing with str_increment() function, the string becomes: b0

Example 8:

<?php
  $string = "z9";
  echo "The original string is: " . $string . nl2br("\n");
  echo "After incrementing with str_increment() function, the string becomes: " . str_increment($string); 
?>

Output:

The original string is: z9
After incrementing with str_increment() function, the string becomes: aa0

Notes on str_increment() Function:

  • You can think of the str_increment() as a string based counter.

Difference Between str_increment() and Auto-increment(++):

  • The str_increment() changes the original string, on the other hand, auto-increment changes the original string. Check following example-
    <?php
      $string = "sow";
      echo "The original string is: " . $string . nl2br("\n");
      echo "After incrementing with str_increment() function, the string becomes: " . str_increment($string); 
      echo nl2br("\n"); 
      echo "After incrementing, the original string is: " . $string;
      echo nl2br("\n");  
      $string = "sow";
      echo "The original string is: " . $string . nl2br("\n");
      $string++;
      echo "After incrementing with auto-increment, the original string becomes: " . $string;
    ?>
    

    Output:

    The original string is: sow
    After incrementing with str_increment() function, the string becomes: sox
    After incrementing, the original string is: sow
    After incrementing with auto-increment, the original string becomes: sox
  • The str_increment() is safe and dependable when it comes to increment an alphanumeric string. On the other hand, auto-increment (++) sometimes provides bizarre result.

PHP Version Support:

PHP 8.3+

Summary: PHP str_increment() Function

str_increment() function is one of the built-in string function.  Use this function when you need to increment an alphanumeric string.

Reference:

https://www.php.net/manual/en/function.str-increment.php