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 #176669 is a reply to message #176657] Mon, 16 January 2012 10:48 Go to previous messageGo to previous message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma:
Senior Member
El 16/01/2012 2:10, lb escribió/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?

Interesting subject... There are actually two different sections in the
PHP manual:

*************************************************************************

Explicit cast:

http://es2.php.net/manual/en/language.types.integer.php#language.types.inte ger.casting

«To explicitly convert a value to integer, use either the (int) or
(integer) casts. [...] A value can also be converted to integer with the
intval() function.»

It admit it's not 100% clear but I have the impression that the
underlying code is just the same and intval():

http://es2.php.net/manual/en/function.intval.php>

*************************************************************************

Implicit cast:

http://es2.php.net/manual/en/language.types.string.php#language.types.strin g.conversion

«When a string is evaluated in a numeric context, the resulting value
and type are determined as follows.»

First difference is that you don't necessarily get an integer:

«If the string does not contain any of the characters '.', 'e', or 'E'
and the numeric value fits into integer type limits (as defined by
PHP_INT_MAX), the string will be evaluated as an integer. In all other
cases it will be evaluated as a float.»

Second difference is that a standard C library is mentioned:

«For more information on this conversion, see the Unix manual page for
strtod(3).»

.... thus we can get further details:

http://linux.die.net/man/3/strtod

«The expected form of the (initial portion of the) string is optional
leading white space as recognized by isspace(3), an optional plus ('+')
or minus sign ('-') and then either (i) a decimal number, or (ii) a
hexadecimal number, or (iii) an infinity, or (iv) a NAN (not-a-number).»

*************************************************************************

I believe the most relevant difference is that:

- intval() accepts any base but does not guess (defaults to 10)
- strtod() only accepts base 10 and 16 and does guess

.... which explains your issue.

--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
[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: Fri Sep 20 16:38:25 GMT 2024

Total time taken to generate the page: 0.05058 seconds