$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';一个++;变成'['('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复活节彩蛋?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!