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 ] )
What is the difference between (int) and intval()?
intval()
accepts a second argument ($base
) while (int)
doesn't.
int intval( mixed $var [, int $base = 10 ] )
But as long as you call intval()
with just 1 argument (like in your case), there's no difference.
When should one use intval and when int
intval
is slower than casting toint
(it's a function)intval
can accept a base if its first parameter is a string
Since 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
.
PHP - Purpose of intval()?
intval()
lets you specify a base to convert from.
$decimal_value = intval("FF", 16)
For simple base-10 conversions, there's no reason to use intval()
.
Is (int)$var same as intval($var)?
(int)
would seem to be a bit faster than intval
, as you don't have the overhead of a function call. intval
also allows you to set an optional base to convert to, which might be useful:
int intval ( mixed $var [, int $base = 10 ] )
In PHP, why use inval() rather than casting with (int)?
In contrast to casting, intval
also allows non-decimal bases.
Additionally, every programmer knows functions and can easily look them up at php.net/function
, whereas casts are not as widely known.
Best Option for integer validation: intval() or filter_input() or casting int's
You would want to use FILTER_VALIDATE_INT
EDIT: with the option min_range
of 1
assuming you are using an AUTO_INCREMENT
value for user_id
in your database
if($user_id = filter_input(INPUT_POST, 'user_id', FILTER_VALIDATE_INT, 1)){
//success
}
This validates it is an integer and then returns a cleaned variable if validation passes.
How do I convert a String to an int in Java?
String myString = "1234";
int foo = Integer.parseInt(myString);
If you look at the Java documentation you'll notice the "catch" is that this function can throw a NumberFormatException
, which you can handle:
int foo;
try {
foo = Integer.parseInt(myString);
}
catch (NumberFormatException e) {
foo = 0;
}
(This treatment defaults a malformed number to 0
, but you can do something else if you like.)
Alternatively, you can use an Ints
method from the Guava library, which in combination with Java 8's Optional
, makes for a powerful and concise way to convert a string into an int:
import com.google.common.primitives.Ints;
int foo = Optional.ofNullable(myString)
.map(Ints::tryParse)
.orElse(0)
How do I parse a string to a float or int?
>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545
Related Topics
Access Controller Method from Another Controller in Laravel 5
Jquery - Call Ajax Every 10 Seconds
A Problem Occurred Somewhere in the Ssl/Tls Handshake
Export MySQL Data to Excel in PHP
PHP Check If File Contains a String
PHP 5 Strpos() Difference Between Returning 0 and False
PHP Difference Between Array() and []
How to Use PHPexcel to Read Data and Insert into Database
How to Import a .SQL File in MySQL Database Using PHP
Smtp Connect() Failed PHPmailer - PHP
Alternative to MySQL_Real_Escape_String Without Connecting to Db