Increment behavior on strings - PHP easter egg?

坚强是说给别人听的谎言 提交于 2019-11-28 13:40:47

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

Its officially documentated http://php.net/language.operators.increment

It's not an Easter egg. It's expected in PHP, but no it's not common in other languages. (At least not incrementing letters.) PHP treats strings containing a number the same as numbers in most cases. So you can also "2" * "2" for example.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!