php 非运算 奇怪,php 5.3中++运算符的奇怪行为

在PHP中,可以增加字符串(但不能使用加法运算符“增加”字符串,因为加法运算符将导致字符串强制转换为

int

,您只能使用递增运算符来“递增”字符串!…请参阅最后一个示例):

所以

"a" + 1

"b"

之后

"z"

"aa"

等等。

所以之后

"Test"

"Tesu"

在使用PHP的自动类型强制时,必须注意上面的内容。

自动类型强制:

$a="+10.5";

echo ++$a;

// Output: 11.5

// Automatic type coercion worked "intuitively"

?>

没有自动类型强制!(增加字符串):

$a="$10.5";

echo ++$a;

// Output: $10.6

// $a was dealt with as a string!

?>

如果你想处理ASCII字母序列,你必须做一些额外的工作。

如果要将字母转换为其ASCII序号,请使用

$a="Test";

foreach(str_split($a) as $value)

{

$a += ord($value); // string + number = number

// strings can only handle the increment operator

// not the addition operator (the addition operator

// will cast the string to an int).

}

echo ++$a;

?>

上面利用了这样一个事实:字符串只能在PHP中递增。不能使用加法运算符增加它们。对字符串使用加法运算符将使其强制转换为

int

因此:

不能使用加法运算符“增加”字符串:

$a = 'Test';

$a = $a + 1;

echo $a;

// Output: 1

// Strings cannot be "added to", they can only be incremented using ++

// The above performs $a = ( (int) $a ) + 1;

?>

以上将尝试铸造“

Test

“致

(int)

添加前

1

. 铸造”

试验

“致

(int)

结果

0

.

注意:您

不能减少字符串

:

请注意,字符变量可以递增,但不能递减,因此只支持纯ASCII字符(A-Z和A-Z)。

前面的意思是

echo --$a;

将实际打印

试验

根本不需要更改字符串。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值