Re: Implied cast differs from explicit cast [message #176662 is a reply to message #176660] |
Mon, 16 January 2012 07:42 |
Arno Welzel
Messages: 317 Registered: October 2011
Karma:
|
Senior Member |
|
|
Jerry Stuckle, 2012-01-16 04:39:
> On 1/15/2012 10:08 PM, lb wrote:
>> jstucklex(at)attglobal(dot)net wrote:
>>> ...
>>> No, it is not. '0x12' is a hex number, not an integer. (int)'0x12'
>>> converts this correctly to 0.
>>
>> OK, but if (int)'0x12' is 0, why is '0x12'+0 == 18?
>>
>> Why are they different? Aren't they both doing 'string conversion to
>> number'?
>
> As I said - '0x12' is a hex value, not an integer. You are trying to
> convert it as if it were a string representation of an integer. The
> implicit conversion recognizes it is a hex value and uses the
> appropriate conversion.
There is no difference between "hex value" and "integer" - both 0x12 and
18 are just different representations of the same value. The problem is
that the explizit cast from string to int does not take this into account.
Example:
<?php
$a = 0x12;
$b = 18;
print $a-$b;
?>
Result:
0
--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
|
|
|