Incrementing behavior in strings - PHP easter egg?

$var = 'test_1'; var_dump(++$var); // string(6) "test_2" $var2 = '1_test'; var_dump(++$var2); // string(6) "1_tesu" $var3 = 'test_z'; var_dump(++$var3); // string(6) "test_a" $var4 = 'test_'; var_dump(++$var4); // string(5) "test_" 

Thus, apparently, the use of the increment operator in a string leads to an increase in the number if the last character is a number, increasing the letter, and then resetting z once if the last character is in the alphabet and has no effect on non-aviation numeric characters.

Is this the standard feature expected in many scripting languages, or did I just find a PHP Easter egg?

+4
source share
3 answers

PHP follows the Perl convention when dealing with arithmetic operations on character variables rather than C. For example, in PHP and Perl $ a = 'Z'; $ A ++; turns $ a into 'AA', and into C a = 'Z'; A ++; turns into '[' (ASCII 'Z' value is 90, ASCII '[' value is 91). Please note that character variables can be increased, but not reduced, and even simple ASCII characters (az and AZ) are even supported. The increment / decrement of other character variables is not affected, the original string is not changed.

-> http://php.net/manual/en/language.operators.increment.php

+5
source
+2
source

This is not an easter egg. This was expected in PHP, but no, it is not common in other languages. (At least not increasing the letters.) In most cases, PHP processes strings containing a number, the same as numbers. So you can also "2" * "2" .

0
source

Source: https://habr.com/ru/post/1391316/


All Articles