str_decrement() Function in PHP 8.4 and PHP 8.5 With Example

str_decrement() is a native PHP function introduced in PHP 8.3, and its behavior remains exactly the same in PHP 8.4 and 8.5. It decrements an alphanumeric ASCII string, similar to how works on numbers, but with "borrowing" logic for letters and digits.

Functionality

It takes an alphanumeric string and returns the "previous" string in sequence .

Example 1: Simple Decrement

<?php
$str = ABC ;
var_dump(str_decrement($str));
// Output: string(3) "ABB"
?>

Here, C decrements to ‘B’.

Example 2: With Borrowing (Carry)

<?php
$str = ZA ;
var_dump(str_decrement($str));
// Output: string(2) "YZ"

$str = AA ;
var_dump(str_decrement($str));
// Output: string(1) "Z"
?>

When the last character is A (or 0 ), it "borrows" from the preceding character. ZA becomes YZ because A borrows from Z , turning it into Y and the A into Z . Similarly, AA borrows to become just Z .

9 Views