Understanding intval() and types conversion

From: Date: Tue, 25 Jan 2005 19:02:35 +0000
Subject: Understanding intval() and types conversion
Groups: php.general 
Request: Send a blank email to php-general+get-207276@lists.php.net to get a copy of this message
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.

« previous php.general (#207276) next »