Re: Understanding intval() and types conversion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jordi Canals wrote:
Hi,

I'm trying to understand how the intval() function works, but I'm

you'd think offhand that that would be easy....

unable to understand how exactly it works.


hmm. very odd. I took a look at your code and did a few more tests on PHP 5.0.2 (cli) (built: Oct 21 2004 13:52:27):

<OUTPUT>

$a = (0.1 + 0.7) * 10;var_dump($a);
------------
float(8)

$b = intval($a);var_dump($b);
------------
int(7)

$c = $a;settype($c, "integer");var_dump($c);
------------
int(7)

$d = intval("$a");var_dump($d);
------------
int(8)

$e = floatval($a);var_dump($e);
------------
float(8)

$f = floatval("$a");var_dump($f);
------------
float(8)

$g = (int) floatval($a);var_dump($g);
------------
int(7)

$h = $a;settype("$h", "integer");var_dump($h);
------------
int(8)

------------
var_dump($a,$b,$c,$d,$e,$f,$g,$h,"$a");
------------
float(8)
int(7)
int(7)
int(8)
float(8)
float(8)
int(7)
int(8)
string(1) "8"


</OUTPUT> <CMD-LINE-CODE>

php -r '

$a = (0.1 + 0.7) * 10;

echo "\n\$a = (0.1 + 0.7) * 10;var_dump(\$a);\n------------\n";
var_dump($a);

$b = intval($a);
echo "\n\$b = intval(\$a);var_dump(\$b);\n------------\n";
var_dump($b);

$c = $a;settype($c, "integer");
echo "\n\$c = \$a;settype(\$c, \"integer\");var_dump(\$c);\n------------\n";
var_dump($c);

$d = intval("$a");
echo "\n\$d = intval(\"\$a\");var_dump(\$d);\n------------\n";
var_dump($d);

$e = floatval($a);
echo "\n\$e = floatval(\$a);var_dump(\$e);\n------------\n";
var_dump($e);

$f = floatval("$a");
echo "\n\$f = floatval(\"\$a\");var_dump(\$f);\n------------\n";
var_dump($f);

$g = (int) floatval($a);
echo "\n\$g = (int) floatval(\$a);var_dump(\$g);\n------------\n";
var_dump($g);

$h = "$a";settype($h, "integer");
echo "\n\$h = \$a;settype(\"\$h\", \"integer\");var_dump(\$h);\n------------\n";
var_dump($h);

echo "------------\n\nvar_dump(\$a,\$b,\$c,\$d,\$e,\$f,\$g,\$h,\"\$a\");\n------------\n";

var_dump($a,$b,$c,$d,$e,$f,$g,$h,"$a");

'

</CMD-LINE-CODE>

Take a look at this piece of code (Tested on PHP 5.0.3):

<?php

$a = (0.1 + 0.7) * 10;
$b = intval($a);

echo 'a -> '. $a .' -> '. gettype($a);  // Prints: a -> 8 -> double
echo '<br>';
echo 'b -> '. $b .' -> '. gettype($b);  // Prints: b -> 7 -> integer

sidenote: if you use double quotes in your test code you make it a little easier for people to run the code off the cmdline ('php -r' on *nix).


?>

I also tested settype() and casting:

settype($a, 'integer'); // New value for $a is 7
$b = (int) $a;   // Value for $b is 7

I cannot understand it. If originally the value for $a is 8 (double),
why when converting to integer I get 7?

my guess this is a round error issue - never noticed this problem before... this is a problem right? anybody?


Any help or comment which helps me to understand that will be really welcome! Jordi.


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux