Understanding intval() and types conversion

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

 



Hi,

I'm trying to understand how the intval() function works, but I'm
unable to understand how exactly it works.

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

?>

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?

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