开发者

Strange behaviour of ++ operator in PHP 5.3

开发者 https://www.devze.com 2023-01-17 21:26 出处:网络
Watch following code: $a = \'Test\'; echo ++$a; This will output: Tesu Question is, why ? I know "u" is after "t&qu开发者_如何转开发ot;, but why it doesn\'t print "1" ???

Watch following code:

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

This will output:

Tesu

Question is, why ?

I know "u" is after "t&qu开发者_如何转开发ot;, 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.


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


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.



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

0

精彩评论

暂无评论...
验证码 换一张
取 消