Want to know what is the difference between using Intval () and (int) conversions? Or what is the difference between the two, including function, definition. or use frequency, efficiency and so on.
The code is as follows:
<?php
echo "<br/>Numerical conversion:";
$string="2a";
$string1=intval($string);
echo'$string1 value:'.$string1.'$string2 value:';//Single quotes will not output the variable, it will be output as it is
$string2=(int)($string);
echo $string2
?>
Not found in the manual.
This is also said in the Handbook: references:
int intval (mixed $var [, int $base])
Returns the integer value of the variable var by using a specific binary conversion (by default, decimal). If this is the only difference, then I would like to use (int) to deal with the 10 binary situation is a good choice?
no difference, general (int), plus float, string, array, etc.
Intval (), if the argument is a string, the integer value represented by the number string before the first character in the string that is not a number is returned. If the first string of strings is '-', the second begins.
If the argument is a number of character points, the value after the rounding is returned.
Of course, the value returned by Intval () is within the range represented by a 4 byte ( -2147483648~2147483647), and the value beyond that range will be replaced with the boundary value.
Example: Intval ("A") = 0; Intval (12.3223) = 12; Intval ("1123asdfka3243") = 1123;
int ();
Cases:
$a = 0.13;
$b = (int) $a; $b = 0;
$a = 0.99;
$b = (int) $a; $b = 0;
$a = 1.01;
$b = (int) $a; $b = 1;
$a = 1.99;
$b = (int) $a; $b = 1;
PHP string converted to int
Sometimes, it is important to have the value of a variable in int format. Eaxmple, if your visitor fills out the form, with the age of the field, this should be an int. However, in the $ _post array, you take it as a string.
It is easy to convert the PHP string to int. We need to use the variable type casting before you. So you need to use (INT). Here's an example of how to do this:
The code is as follows:
<?php
$str = "10";
$num = (int)$str;?>
If the code to be checked realy works, we can use the = = = operator. This operator checks not only the value but the type as well. The code should look like this:
The code is as follows:
<?php
$str = "10";
$num = (int)$str;
if ($str === 10) echo "String";
if ($num === 10) echo "Integer";
?>
There is one more issue that is open. If our string is not a mere numeric string, what happens. I mean there are other characters in the string. In this case, the conversion operation tries the best and can convert the string if only the space is there, if there is no valid character after the numeric value. It works as follows:
"Ten"-> 10
"10.5"-> 10
"10,5"-> 10
"Ten"-> 10
"Ten"-> 10
"10test"-> 10
"Test10"-> 0