Increment behavior on strings - PHP easter egg?

后端 未结 3 374
灰色年华
灰色年华 2020-12-11 17:24
$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);         


        
3条回答
  •  醉酒成梦
    2020-12-11 17:55

    PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's. 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.

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

提交回复
热议问题