Copy Code code as follows:
<?php
echo "<br/> numeric cast:";
$string = "2a";
$string 1=intval ($string);
Echo ' $string value of 1: '. $string 1. ' Value of $string 2: ';//single quotation mark does not output variable, output as-is
$string 2= (int) ($string);
Echo $string 2
?>
I can't find it in the handbook.
And that's what the manual says: quote:
int intval (mixed $var [, int $base])
Returns the integer value of the variable var by using a specific binary conversion (the default is decimal). If this is the only difference, then I like to use (int) to deal with 10 of the situation is a good choice?
no difference, general (int), plus float, string, array, etc.
Intval (), if the argument is a string, returns the integer value represented by the number string before the first character in the string that is not a number. If the string first is '-', start at the second.
If the argument is a number of points, the value after which he rounded is returned.
Of course intval () returns a value that is within a 4-byte range ( -2147483648~2147483647), and a value that exceeds that range will be replaced with a 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;
convert PHP string to int
Sometimes it is important to have the value of a variable in int format. Eaxmple, if your visitor fills out the form, along with the age of the field, this should be an int. However, in the $ _post array, you take it as a string.
The PHP string that is converted to int is easy. We need to use the variable type casting before you. So you need to use (INT). Here is an example of how to do this:
Copy Code code as follows:
<?php
$str = "10";
$num = (int) $str;? >
If you want to check the code realy engineering, we can use the = = operator. This operator checks not only the value but the type as well. Such a code would look like this:
Copy Code code as follows:
<?php
$str = "10";
$num = (int) $str;
if ($str = =) echo "String";
if ($num = =) echo "Integer";
?>
There is another problem that is open. If our string is not a mere string of numbers, 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. Its working principle is as follows:
"Ten"-> 10
"10.5"-> 10
"10,5"-> 10
"Ten"-> 10
"Ten"-> 10
"10test"-> 10
"Test10"-> 0