PHP str_increment() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP 8.3 and later, str_increment() moves an alphanumeric string one step forward.

It follows Perl-style rules, so "a" becomes "b" and "z" becomes "aa".

Syntax

Call str_increment() with the values you need. Start with this line.

PHP
$next = str_increment($label);
CallWhat it does
str_increment("a")Returns "b".
str_increment("Az")Returns "Ba".

Only letters a-z, A-Z, and digits 0-9 are allowed. Use it for IDs and spreadsheet column names.

Library declarations
PHP
str_increment(string $string): string

Parameters

ParameterDescription
stringRequired. A non-empty alphanumeric string.

Return Value

Return ValueDescription
stringThe next string in the sequence.

PHP Examples

Example 1 — Step Forward One Letter

Each call moves the string one step ahead.

PHP
<?php
echo str_increment("a"), PHP_EOL;
echo str_increment("Az"), PHP_EOL;

How It Works

  1. Pass a letter. a becomes b.
  2. Carry when needed. z wraps to a and the letter before it grows.
  3. Case is kept. Az becomes Ba.
  4. Always returns a string. Even for digits.

Example 2 — Growing Strings and Invalid Input

The string can get longer, and bad input throws an error.

PHP
<?php
echo str_increment("zz"), PHP_EOL;
echo str_increment("a9"), PHP_EOL;

try {
    echo str_increment("");
} catch (ValueError $e) {
    echo "Empty string is not allowed", PHP_EOL;
}

How It Works

  1. Wrap past the end. zz becomes aaa.
  2. Digits roll over too. a9 becomes b0.
  3. Try an empty string. There is nothing to increment.
  4. Catch the ValueError. Print a clear message.

Example 3 — Spreadsheet Column Names

Generate column labels in a loop.

PHP
<?php
$col = "X";
$cols = [];

for ($i = 0; $i < 5; $i++) {
    $cols[] = $col;
    $col = str_increment($col);
}

echo implode(", ", $cols), PHP_EOL;

How It Works

  1. Start at X. Store it in an array.
  2. Increment each time. The loop runs five times.
  3. Z rolls to AA. Just like a spreadsheet.
  4. Print the list. Names are joined with commas.

Frequently Asked Questions

str_increment() returns the next string in an alphanumeric sequence, such as "a" to "b", "z" to "aa", or "a9" to "b0".
str_increment(string $string): string. It takes one non-empty alphanumeric string.
PHP 8.3. It is not available in earlier versions.
It throws a ValueError for an empty string or a string that has characters other than letters and digits.
str_increment() is strict and only accepts alphanumeric strings. The ++ operator on strings has looser, older rules and can behave in surprising ways.

Next: str_ireplace()

str_ireplace() replaces every occurrence of a search value inside a string, ignoring upper.

str_ireplace() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful