tags:

views:

197

answers:

3

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" ???

EDIT: Becouse Zend books teach following:

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.

+19  A: 

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

salathe
+3  A: 

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.


Peter Ajtai
Good edit,but, to put a fine point to it, the string is "added to", i.e. in your code 1 is added to 0 (the post-cast value of "Test). For example try echo '7test' + 0;
GZipp
@GZipp - Well, how would you explain the difference? "Strings can only be added to using the increment operator, but not the addition operator."?
Peter Ajtai
I think "added to" there means concatenation.
GZipp
@Peter Ajtai: `$a + 1` performs an implicit type cast on `$a` before adding `1`, because there are two explicit operands in that expression and `+` is as you said not the concatenation operator in PHP. Thus it's equivalent to `(int)$a + 1`.
BoltClock
I know what the concatenation operator in PHP is. And I misread your previous post. My apologies. I was trying to say that in your code the addition is effectively 0 + 1, which I'm sure you know.
GZipp
@GZipp - Thanks for pointing that out. I was just trying to come up with a succinct but accurate way to explain all that....
Peter Ajtai
I was also trying to say that a string can be added to (mathematically) in this sense: "7test" + 1 will return 8.
GZipp
@GZipp - But "7test" is cast to the int 7 before one is added to it, so you're actually performing the addition operation on an int and not on a string. --------- `gettype("7test" + 1)` will return `integer`. --------- It just depends on the result of the cast, for example `"test7" + 1` will return 1.
Peter Ajtai
Yeah, Peter, that's why I used the phrase "in this sense".
GZipp
+2  A: 

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

BoltClock