Is there any particular difference between intval and casting to int - `(int) X`?
intval()
can be passed a base from which to convert. (int)
cannot.
int intval( mixed $var [, int $base = 10 ] )
One thing to note about the difference between (int)
and intval()
: intval()
treats variables which are already int
s and float
s as needing no conversion, regardless of the base argument (as of PHP 5.3.5 at least). This behavior isn't the most obvious, as noted in the comments on the PHP doc page and shamelessly reiterated here:
$test_int = 12;
$test_string = "12";
$test_float = 12.8;
echo (int) $test_int; // 12
echo (int) $test_string; // 12
echo (int) $test_float; // 12
echo intval($test_int, 8); // 12 <-- WOAH!
echo intval($test_string, 8); // 10
echo intval($test_float, 8) // 12 <-- HUH?