<?php
// conditionally define PHP_INT_MIN since PHP 5.x doesn't
// include it and it's needed to validate integer casts
if (!defined("PHP_INT_MIN")) {
define("PHP_INT_MIN", ~PHP_INT_MAX);
}
echo "Should pass" . PHP_EOL;
var_dump(to_int("0"));
var_dump(to_int(0));
var_dump(to_int(0.0));
var_dump(to_int("0.0"));
var_dump(to_int("10"));
var_dump(to_int(10));
var_dump(to_int(10.0));
var_dump(to_int("10.0"));
echo "Should fail" . PHP_EOL;
var_dump(to_int(1.5));
var_dump(to_int("1.5"));
var_dump(to_int("10abc"));
var_dump(to_int("abc10"));
var_dump(to_int(INF));
var_dump(to_int(-INF));
var_dump(to_int(NAN));
var_dump(to_int(PHP_INT_MAX * 2));
var_dump(to_int(null));
var_dump(to_int(true));
var_dump(to_int(false));
var_dump(to_int(new stdClass()));
var_dump(to_int(fopen("data:text/html,foobar", "r")));
var_dump(to_int([]));
/**
* Returns the value as an int, or false if it cannot be safely cast
* @param mixed $val
* @return int
*/
function to_int($val)
{
switch (gettype($val)) {
case "integer":
return $val;
case "double":
if (!is_infinite($val) && !is_nan($val) && $val >= PHP_INT_MIN) {
// due to rounding issues, on 64-bit platforms
// the float must be less than PHP_INT_MAX
if (
(PHP_INT_SIZE === 8 && $val < PHP_INT_MAX) || // valid 64-bit
(PHP_INT_SIZE !== 8 && $val <= PHP_INT_MAX) // valid non-64-bit
) {
// only accept the float if it can be cast to int without data loss
$int = (int) $val;
if ($int == $val) {
return $int;
}
}
}
return false;
case "string":
$val = trim($val, " \t\n\r\v\f"); // trim whitespace
$float = filter_var($val, FILTER_VALIDATE_FLOAT);
if ($float !== false) {
// only accept the float if it can be cast to int without data loss
$int = (int) $float;
return ($int == $float) ? $int : false;
}
default:
return false;
}
}