Re: Implied cast differs from explicit cast [message #176658 is a reply to message #176657] |
Mon, 16 January 2012 01:51 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 1/15/2012 8:10 PM, lb wrote:
> When converting some strings to int (or double), I am finding that
> an implied cast get different results from explicit cast. This happens
> when the string contains a hex representation. The implied cast converts
> the hex value, but the explicit cast returns 0. The manual under "String
> conversion to numbers" says nothing about it allowing hex values. This is
> with PHP-5.3.9 and older.
>
> Script:
> <?php
> $s1 = '0x12';
> $t1 = $s1 + 0;
> echo "Implied cast t1=$t1\n";
> $t2 = (int)$s1 + 0;
> echo "Explicit cast t2=$t2\n";
>
> Output:
> Implied cast t1=18
> Explicit cast t2=0
>
>
> I think this is odd. A side problem is that is_numeric('0x12') is true, but
> ((int)'0x12') is 0. Does anyone think this a bug?
>
>
No, it is not. '0x12' is a hex number, not an integer. (int)'0x12'
converts this correctly to 0.
If you want to convert a hex number to an integer, you need to use
hexdec(), which is what the implied conversion does.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|