Modify String using str_increment and str_decrement in PHP 8.3

Modify String using str_increment and str_decrement in PHP 8.3

Since PHP 8.3, the str_increment and str_decrement functions can be employed. The former is designed to increment an alphanumeric string, while the latter enables to decrement an alphanumeric string. These functions can be particularly useful in scenarios where you need to manage alphanumeric strings that represent sequential values.

<?php

echo str_increment('ABC').PHP_EOL; // ABD
echo str_decrement('ABC').PHP_EOL; // ABB
echo str_increment('39').PHP_EOL; // 40
echo str_decrement('38').PHP_EOL; // 37

The ValueError will be thrown by the functions in case the string is empty or is not an alphanumeric ASCII string.

Leave a Comment

Cancel reply

Your email address will not be published.