FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Implied cast differs from explicit cast
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Implied cast differs from explicit cast [message #176658 is a reply to message #176657] Mon, 16 January 2012 01:51 Go to previous messageGo to previous message
Jerry Stuckle is currently offline  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
==================
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Open Source Development
Next Topic: error from phpadmin
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 19:54:17 GMT 2024

Total time taken to generate the page: 0.08939 seconds