字符串的递增行为-PHP复活节彩蛋? [英] Increment behavior on 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_"
因此,很显然,如果最后一个字符是数字,则对字符串使用增量运算符的作用是增加数字,增加字母,然后如果最后一个字符位于字母中并且没有任何字符,则重置为一次z对非字母数字字符有效.
So apparently, using an increment operator on a string has the effect of increasing the digit if the last character is a number, increasing the letter and then resetting to a once z if the last character is in the alphabet, and has no effect on non alpha numeric characters.
这是许多脚本语言所期望的标准功能吗?还是我刚刚找到了PHP复活节彩蛋?
Is this a standard feature, expected in many scripting languages, or did I just find a PHP easter egg?
推荐答案
PHP在处理算术运算时遵循Perl的约定 在字符变量上,而不是在C上.例如,在PHP和Perl中,$ a = 'Z'; $ a ++;将$ a转换为"AA",而在C中将a转换为"Z"; a ++;变成 '['('Z'的ASCII值为90,'['的ASCII值为91).注意 字符变量可以递增但不能递减,即使如此 仅支持纯ASCII字符(a-z和A-Z). 递增/递减其他字符变量无效, 原始字符串不变.
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
这篇关于字符串的递增行为-PHP复活节彩蛋?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!