This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Fastest way to convert string to integer in PHP
PHP: Is there any particular difference between intval and (int)?
Is (int)$var same as intval($var) ?
Apparently they both do the same thing.
Is there any situation in which they would return different results?
(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 ] )
Related
This question already has answers here:
How do I convert a string to a number in PHP?
(35 answers)
Closed 2 years ago.
I have a value like 12,25,246
I want to get 1225246 (must be an integer)
How can I do that?
I have searched a lot but there are direct answer like converting string to integer but not like this actually these both are like integer
I have tried php formate_number but it did not worked.
You could use a combination of intval() and str_replace() to do this.
Example:
$value = '12,25,246';
var_dump(intval(str_replace(',','',$value)));
// Yields: "int(1225246)"
Sandbox
$number = (int)str_replace(',', '', '12,25,246');
here is it
This question already has an answer here:
Typecasting vs function to convert variable type in PHP
(1 answer)
Closed 6 years ago.
I've got some strings that I need to cast as floats but have seen two apparently different ways of doing it:
$float = floatval ($float);
and
$float = (float) $float;
Are there any differences between the two methods?
In all other cases it will be evaluated as a float. In other words, the $string is first interpreted as INT, which cause overflow (The $string value 2968789218 exceeds the maximum value ( PHP_INT_MAX ) of 32-bit PHP, which is 2147483647.), then evaluated to float by (float) or floatval()
Please have look at PHP Convert String into Float/Double
The best answer for this is Typecasting vs function to convert variable type in PHP
This question already has answers here:
php converting formatted int to int
(2 answers)
Closed 6 years ago.
I am parsing a stream of strings like '1,985' and I want to convert them to the respective integer, like 1985 in this case.
Is there any built-in/efficient PHP function which does that?
Use intval and str_replace for getting your desire result. Intval for your integer number and str_replace for remove the comma from string.
$str = '1,985';
var_dump(intval(str_replace(",", "", $str))); //int(1985)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php validate integer
Currently I'm using this method:
Validate the input using isset($val) && is_numeric($val)
Cast it to int and passing it to the function: DoSomething((int)$val);
So my question is this: Is this the fastest and most practical way to do this? If I cast the value to int, is the int range enough for the maximum number of rows in the mysql database?
PHP offers integrated basic filter & validation functions.
it seems the method is quite redundant.
either validate or cast - no need to do both.
And this is apparently not a place that needs to be "fastest".
a quick look at php manual page shoved that:
$foo = 5a;
settype($foo, "integer"); // $foo is now 5 (integer)
And:
/* checks if a string is an integer with possible whitespace before and/or after, and also isolates the integer */
$isInt=preg_match('/^\s*([0-9]+)\s*$/', $myString, $myInt);
This question already has answers here:
Is there any particular difference between intval and casting to int - `(int) X`?
(7 answers)
Closed 8 years ago.
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 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.
All behaviour explained here along with GOTCHAS...
http://php.net/manual/en/function.intval.php
http://www.php.net/manual/en/language.types.type-juggling.php