增量字符有效,但添加无效.为什么? [英] Increment character works but adding doesn't. Why?

查看:88
本文介绍了增量字符有效,但添加无效.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$a = 'a';
print ($a+1);
print ($a++);
print $a;

输出为:1 a b

因此很明显增量运算符完成了它的工作,但是我不明白为什么在$a+1情况下输出为'1'的原因.谁能解释?

So it is clear that increment operator did its job but I don't understand why output is '1' in case $a+1. Can anyone explain?

推荐答案

PHP不是C,所以'a' + 1不是'b'.

PHP is not C, so 'a' + 1 is not 'b'.

'a'00+1 = 1.

php> echo (int)'a';
0

后缀/前缀增量运算符 do 的工作方式就像是C字符一样,这​​似乎是PHP的一个讨厌的功能".尤其是因为在这种情况下,减量运算符是空操作.

The fact that the postfix/prefix increment operators do work like if it was a C char seems to be a nasty "feature" of PHP. Especially since the decrement operators are a no-op in this case.

当您增加'z'时,情况会更糟:

When you increment 'z' it gets even worse:

php> $a = 'z';
php> echo ++$a
aa

这篇关于增量字符有效,但添加无效.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆