gpt4 book ai didi

php - 字符串的增量行为 - PHP 复活节彩蛋?

转载 作者:可可西里 更新时间:2023-11-01 13:18:17 26 4
gpt4 key购买 nike

$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,并且没有对非字母数字字符的影响。

这是许多脚本语言所期望的标准功能,还是我只是找到了一个 PHP 彩蛋?

最佳答案

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 复活节彩蛋?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8885440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com