php8.5
Home/ Manual/ strings / functions/ str_increment

str_increment

PHP function Edit on GitHub ✎

(PHP 8 >= 8.3.0)

Increment an alphanumeric string

Description

str_increment(string $string): string

Returns the incremented alphanumeric ASCII string.

Parameters

string

The input string.

Return Values

Returns the incremented alphanumeric ASCII string.

Errors/Exceptions

A ValueError is thrown if string is empty.

A ValueError is thrown if string is not an alphanumeric ASCII string.

Examples

Basic str_increment() example

php
<?php
$str = 'ABC';
var_dump(str_increment($str));
?>

The above example will output:

output
string(3) "ABD"

str_increment() example with a carry

php
<?php
$str = 'DZ';
var_dump(str_increment($str));

$str = 'ZZ';
var_dump(str_increment($str));
?>

The above example will output:

output
string(2) "EA"
string(3) "AAA"

See Also

Source: reference/strings/functions/str-increment.xml · from the official PHP manual (php/doc-en)