++ 的奇怪行为PHP 5.3 中的运算符

发布于 2024-09-24 16:38:30 字数 281 浏览 6 评论 0原文

观看以下代码:

$a = 'Test';
echo ++$a;

这将输出:

Tesu

问题是,为什么?

我知道“u”在“t”之后,但为什么它不打印“1”???

PHP 文档:

此外,变量正在递增 或递减将转换为 适当的数值数据 类型——因此,下面的代码将 返回 1,因为字符串 Test 是 首先转换为整数 0,然后递增。

Watch following code:

$a = 'Test';
echo ++$a;

This will output:

Tesu

Question is, why ?

I know "u" is after "t", but why it doesn't print "1" ???

PHP Documentation:

Also, the variable being incremented
or decremented will be converted to
the appropriate numeric data
type—thus, the following code will
return 1, because the string Test is
first converted to the integer number
0, and then incremented.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故乡的云 2024-10-01 16:38:30

在处理字符变量而不是 C 的算术运算时,PHP 遵循 Perl 的约定。例如,在 Perl 中 'Z'+1 变成 'AA',而在 C 中 'Z'+1 变成 '[' ( ord('Z') == 90, ord('[') == 91 ) 。请注意,字符变量可以递增,但不能递减,即使如此,也仅支持纯 ASCII 字符(az 和 AZ)。

来源:http://php.net/operators.increment

PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's. For example, in Perl 'Z'+1 turns into 'AA', while in C 'Z'+1 turns into '[' ( ord('Z') == 90, ord('[') == 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.

Source: http://php.net/operators.increment

我三岁 2024-10-01 16:38:30

在 PHP 中,您可以递增字符串(但不能使用加法运算符“增加”字符串,因为加法运算符会导致字符串转换为 int,因此您只能使用递增运算符来“增加”字符串!...参见最后一个示例):

因此 "a" + 1"b""z" 出现之后 <代码>“aa”等等。

因此,在 "Test" 之后是 "Tesu"

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

自动类型强制:

<?php
$a="+10.5";
echo ++$a;

// Output: 11.5
//   Automatic type coercion worked "intuitively"
?>

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

<?php
$a="$10.5";
echo ++$a;

// Output: $10.6
//   $a was dealt with as a string!
?>


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

如果您想将字母转换为 ASCII 序数,请使用 ord(),但这一次仅适用于一个字母。

<?php
$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,因此:

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

<?php
   $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。将“Test”转换为 (int) 结果为 0


注意:您不能递减字符串

请注意,字符变量可以递增,但不能递减,即使如此,也仅支持纯 ASCII 字符(az 和 AZ)。

前面的意思是 echo --$a; 实际上会打印 Test 而不改变字符串。


In PHP you can increment strings (but you cannot "increase" strings using the addition operator, since the addition operator will cause a string to be cast to an int, you can only use the increment operator to "increase" strings!... see the last example):

So "a" + 1 is "b" after "z" comes "aa" and so on.

So after "Test" comes "Tesu"

You have to watch out for the above when making use of PHP's automatic type coercion.

Automatic type coercion:

<?php
$a="+10.5";
echo ++$a;

// Output: 11.5
//   Automatic type coercion worked "intuitively"
?>

No automatic type coercion! (incrementing a string):

<?php
$a="$10.5";
echo ++$a;

// Output: $10.6
//   $a was dealt with as a string!
?>


You have to do some extra work if you want to deal with the ASCII ordinals of letters.

If you want to convert letters to their ASCII ordinals use ord(), but this will only work on one letter at a time.

<?php
$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;
?>

live example

The above makes use of the fact that strings can only be incremented in PHP. They cannot be increased using the addition operator. Using an addition operator on a string will cause it to be cast to an int, so:

Strings cannot be "increased" using the addition operator:

<?php
   $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;
?>

The above will try to cast "Test" to an (int) before adding 1. Casting "Test" to an (int) results in 0.


Note: You cannot decrement strings:

Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.

The previous means that echo --$a; will actually print Test without changing the string at all.


月依秋水 2024-10-01 16:38:30

PHP 中的增量运算符在内部针对字符串的序数值起作用。在递增之前,字符串不会转换为整数。

The increment operator in PHP works against strings' ordinal values internally. The strings aren't cast to integers before incrementing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文