What happens if ++ operator is used with string value in PHP?

When you use the ++ operator with a string value in PHP, it performs an alphanumeric increment operation. For pure alphabetic strings, it increments the last character to the next letter in the alphabet. For numeric strings, it treats the string as a number and increments it mathematically.

String Increment Behavior

The increment operator follows these rules −

  • For alphabetic characters: increments to next letter (a?b, z?aa)
  • For numeric strings: converts to number and increments
  • For mixed alphanumeric: increments the rightmost character

Example

<?php
    $values = 'John';
    echo "The string modified value is=" . ++$values . "<br>";
    
    $values1 = "10.5";
    echo "The string incremented value is=" . ++$values1;
?>
The string modified value is=Joho
The string incremented value is=11.5

More Examples

<?php
    $str1 = 'A';
    echo "A incremented: " . ++$str1 . "<br>";
    
    $str2 = 'Z';
    echo "Z incremented: " . ++$str2 . "<br>";
    
    $str3 = 'abc';
    echo "abc incremented: " . ++$str3;
?>
A incremented: B
Z incremented: AA
abc incremented: abd

Conclusion

PHP's ++ operator with strings follows alphanumeric increment rules, making it useful for generating sequential identifiers. Numeric strings are treated as numbers, while alphabetic strings increment character by character.

Updated on: 2026-03-15T09:38:13+05:30

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements