PHP 8.3 introduces str_increment() and str_decrement() to predictably increment and decrement alphanumeric strings according to standard base-26/base-36 conventions.
Prior to PHP 8.3, developers relied on the legacy Perl-style string increment operator ($str++). While convenient, it suffered from inconsistent behavior, had no corresponding decrement operator ($str-- did nothing on strings), and frequently produced silent type coercion bugs when strings resembled scientific notation numbers.
PHP 8.3 solves this by introducing two dedicated functions: str_increment() and str_decrement().
Incrementing Alphanumeric Strings
str_increment() steps through ASCII characters (0-9, a-z, A-Z). When a character overflows (such as z rolling over), it wraps around to aa, preserving case and padding conventions:
// Single character rollover
echo str_increment('a'); // 'b'
echo str_increment('z'); // 'aa'
echo str_increment('Z'); // 'AA'
// Multi-character progression
echo str_increment('az'); // 'ba'
echo str_increment('zz'); // 'aaa'
echo str_increment('zzz'); // 'aaaa'
// Mixed alphanumeric strings
echo str_increment('1'); // '2'
echo str_increment('a1'); // 'a2'
echo str_increment('a9'); // 'b0'
echo str_increment('aZ'); // 'bA'
echo str_increment('aZz'); // 'bAa'
Decrementing Alphanumeric Strings
Unlike the old ++ operator, PHP 8.3 introduces a corresponding str_decrement() function that operates in reverse:
// Single character rollbacks
echo str_decrement('b'); // 'a'
echo str_decrement('aa'); // 'z'
echo str_decrement('AA'); // 'Z'
// Multi-character reduction
echo str_decrement('ba'); // 'az'
echo str_decrement('aaa'); // 'zz'
echo str_decrement('aaaa'); // 'zzz'
// Mixed alphanumeric decrement
echo str_decrement('2'); // '1'
echo str_decrement('a2'); // 'a1'
echo str_decrement('b0'); // 'a9'
echo str_decrement('bA'); // 'aZ'
echo str_decrement('bAa'); // 'aZz'
Practical Applications
These functions are particularly helpful for generating sequential human-readable identifiers, spreadsheet-style column names (A, B, ... Z, AA, AB), version codes, or SKU serial numbers:
function generateNextSku(string $currentSku): string
{
// PRD-001A -> PRD-001B
return str_increment($currentSku);
}
Error Handling
Both functions accept only non-empty strings containing ASCII alphanumeric characters ([a-zA-Z0-9]). If an invalid character or empty string is passed, PHP throws a ValueError:
str_increment('item-1'); // Throws ValueError: non-alphanumeric character '-'
str_decrement('a'); // Throws ValueError: 'a' cannot be decremented below 'a'
Summary
- Use
str_increment()to advance alphanumeric strings with consistent case and overflow behavior. - Use
str_decrement()for symmetric reverse stepping. - Replaces legacy
$str++tricks with strict, predictable function calls.