What is the better option:
// Option 1:
$intValue = (int) $numericValue;
// Option 2:
$intValue = intval($numericValue);
Is there any difference between these two lines, and which should be used in which situation?
intval
is slower than casting to int
(it's a function)intval
can accept a base if its first parameter is a stringSince the second item is really obscure, objectively we should say that intval
is simply "worse".
However, personally I like reading intval($var)
better than (int)$var
(the latter may also require wrapping in additional parens) and since if you are converting to int
inside a loop you are definitely doing something wrong, I use intval
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With