5

I am learning PHP for the first time and I find it surprising that the language allows using the increment operator on strings.

     $foo = 'xyZ';
     print ++$foo; // prints xzA

The tutorials I can find around this topic introduce toy examples only. I would be grateful if you mention a situation where using this 'feature' is beneficial.

Thanks in advance!

Helmyano
  • 143
  • 1
  • 7
  • 1
    It's a gimmick, not a feature. SO can't answer the "why" behind language design, except that it got [carried over from Perl](http://stackoverflow.com/questions/6458068/increment-and-decrement-strings-in-perl) – mario Jun 28 '13 at 15:13
  • 2
    The only time I can think of it being useful would be if a series of strings were being tested as conditionals. Instead of writing a test for each one, a for loop would be used to test each one. – asimes Jun 28 '13 at 15:16
  • MS Excel column IDs increment 'A' through 'Z', 'AA' through 'AZ', 'BA' through 'BZ', etc.... matching this character incrementing pattern; so character incrementing is extremely useful when working with Excel worksheets – Mark Baker Jun 29 '13 at 15:27

4 Answers4

4

I would be grateful if you mention a situation where using this 'feature' is beneficial.

This can be a very useful feature ...

Example

$guess = "21661093e56e24cd60b10092005c4ac7";
$next = "aaaa";
$count = 0;
while(md5($next) !== $guess) {
    $next ++;
    $count ++;
}
printf("Found `%s` after %s loops", $next, number_format($count));

Output

Found `baba` after 17,602 loops

I don't intend to crack any PIN or password anytime soon anyway

Baba
  • 94,024
  • 28
  • 166
  • 217
0

have a look at the section named "Example #1 Arithmetic Operations on Character Variables" at http://php.net/manual/en/language.operators.increment.php - You can use the increment operator to add characters and sequences to the string.

Kallum Tanton
  • 802
  • 7
  • 22
0

A charachter is a number actually. You should take a look to the ASCII table http://en.wikipedia.org/wiki/ASCII

Converst number to charachter php documentation http://php.net/manual/en/function.chr.php

$test = 'a'; 
$test++;
echo($test);

As you can see in the ASCII table the numeric representation of 'a' = 97 And of 'b' = 98 Therefor 'b' is displayed

Sander Visser
  • 4,144
  • 1
  • 31
  • 42
0

Taken from the documentation, PHP follows Perl's convention when dealing with arithmetic operations on character variables:

For example, in PHP and Perl $a = 'Z'; $a++; turns $a into 'AA', while in C a = 'Z'; a++; turns a into '[' (ASCII value of 'Z' is 90, ASCII value of '[' is 91). Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported. Incrementing/decrementing other character variables has no effect, the original string is unchanged.

While I personally have not used this feature, I can think of a few benefits by following Perl's convention, as opposed to C's. That is incrementing within the alpha range as opposed to simply incrementing the ASCII value.

By staying within the alpha range, essentially we're talking about a base 26 numeral system (using letters). So just as 9 increments to 10 (carry over), z increments to aa.

What comes to mind immediately would be character hash sequences.

public static function nextSeed() {
   // $seed = abc
   return strtolower(++self::$seed);
   // $seed = abd
}

While likely such seeds would be managed outside of PHP, it is nonetheless beneficial PHP provides natively what would otherwise require more code.

Other use cases:

  • Base-26 Numeral System Representation
  • Calculating character differences
  • Encryption
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174