PHP increment operator

auto-increment, php, string-concatenation

Solution

You are, as Michael says, trying to increment a string - it Does Not Work That Way (tm). What you want to do is this:

<?php
 $s = "pa"; //we're defining the string separately!
 $i = 99; //no quotes, this is a number
 $i++;
 echo $s.$i; //concatenate $i onto $s
?>

There's no automated way to increment a string (aa, ab, etc) the way you're asking. You could turn each letter into a number between 1-26 and increment them, and then increment the previous one on overflow. That's kind of messy, though.

To separate the integer from the string, try this:

PHP split string into integer element and string

Problem

``` <?php $s = "pa99"; $s++; echo $s; ?> ``` The above code outputs to "pb00" What i wanted was "pa100" and so on. But also in case its "pa", I want it to go to "pb" which works well with increment operator.

Original source

Related problems