Increment behavior on strings - PHP easter egg?(字符串的增量行为 - PHP复活节彩蛋?)
问题描述
$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复活节彩蛋?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:字符串的增量行为 - PHP复活节彩蛋?
基础教程推荐
- PHP 类:全局变量作为类中的属性 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何替换eregi() 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
