PHP str_decrement() Function

What is PHP str_decrement() Function?

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

How PHP str_decrement() Function Works

The function decreases the rightmost character to its previous one. So,

  1. For small letters (a-z)-
    • ‘b’ becomes ‘a’. It is applicable for all b-z characters. Check example 1.
    • But for ‘a’-
      • The function reset alphabet to ‘z’ and
      • Moves to the left character and
      • Decrement it. If it is also ‘a’ and the first character, it stops (Check example 9); otherwise, the function repeats from step ‘i’. Check example 2.
      • and if it is the only character in then string, the function returns ValueError.
  2. For capital letters (A-Z)-
    • The same process happens as the capital letters above. Check example 3 & example 4 & example 10.
  3. For numbers (0-9)-
    • ‘1’ becomes ‘0’. It is applicable for all 1-9 characters. Check example 5.
    • But for ‘0’-
      • The function reset digit to ‘9’ and
      • Moves to the left digit and
      • Decrement it. If it is also ‘0’, the function repeats from step ‘i’. Check example 6.
      • and if it is the only character in then string, the function returns ValueError.
  4. For mixed type-
    • The function follows the same rules. Check example 7 & example 8.

Syntax:

str_decrement(string)

Parameters:

The Function has 1 required parameter and 1 optional parameter-

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

Return Values:

The function returns-

  • the decremented 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 decrementing with str_decrement() function, the string becomes: " . str_decrement($string); 
?>

Output:

The original string is: abc
After decrementing with str_decrement() function, the string becomes: abb

Example 2:

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

Output:

The original string is: cba
After decrementing with str_decrement() function, the string becomes: caz

Example 3:

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

Output:

The original string is: CBA
After decrementing with str_decrement() function, the string becomes: CAZ

Example 4:

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

Output:

The original string is: DCA
After decrementing with str_decrement() function, the string becomes: DBZ

Example 5:

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

Output:

The original string is: 123
After decrementing with str_decrement() function, the string becomes: 122

Example 6:

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

Output:

The original string is: 100<br />
After decrementing with str_decrement() function, the string becomes: 99

Example 7:

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

Output:

The original string is: z0
After decrementing with str_decrement() function, the string becomes: y9

Example 8:

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

Output:

The original string is: a0
After decrementing with str_decrement() function, the string becomes: 9

Example 9:

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

Output:

The original string is: aa
After decrementing with str_decrement() function, the string becomes: z

Example 10:

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

Output:

The original string is: AA
After decrementing with str_decrement() function, the string becomes: Z

Notes on str_decrement() Function:

  • You can think of the str_decrement() as a string based counter in reverse order.

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

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

    Output:

    The original string is: sow
    After decrementing with str_decrement() function, the string becomes: sov
    After decrementing, the original string is: sow
    The original string is: sow
    After decrementing with auto-increment, the original string becomes: sox

PHP Version Support:

PHP 8.3+

Summary: PHP str_decrement() Function

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

Reference:

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