Silent Type Cast¶
<?php
//declare(strict_types=1);
function foo(float $x): int {
var_dump($x);
return $x;
}
var_dump(foo(1));
//var_dump(foo(1.1));
An integer is a float, but the opposite is not always true. For PHP, pushing an integer in a function that types float its argument, makes that integer an float. The reverse conversion from float to integer is also possible, at return time. This happens when strict_types is not activated, and when the float has no decimal part, as is the case here.
Otherwise, it is a Type error, or an warning about loss of precision.
See Also¶
Integers to floats, and back [Try me]
PHP Error Messages¶
PHP Features¶
Last updated: 14 July 2026