I would expect all below comparisons to be bool (true) but they are not.
Can anyone explain this?
test.php
<?php
$f = 12;
$f += 5.95;
$f += 5.95;
$f += 5.95;
echo 'var_dump($f) = ';
var_dump($f);
echo 'var_dump($f == '29.85') = ';
var_dump($f == '29.85');
echo 'var_dump($f == 29.85) = ';
var_dump($f == 29.85);
echo 'var_dump($f == (float)'29.85') = ';
var_dump($f == (float)'29.85');
echo 'var_dump($f == '29.85') = ';
var_dump((string)$f == '29.85');
echo 'var_dump(round($f, 2) == '29.85') = ';
var_dump(round($f, 2) == '29.85');
$ php test.php
var_dump($f) = float(29.85)
var_dump($f == '29.85') = bool(false)
var_dump($f == 29.85) = bool(false)
var_dump($f == (float)'29.85') = bool(false)
var_dump($f == '29.85') = bool(true)
var_dump(round($f, 2) == '29.85') = bool(true)
$ php -v
PHP 5.2.14 (cli) (built: Jul 23 2010 15:23:00)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…