PHP intval () and (int) conversion using and Difference _php Tutorial

Source: Internet
Author: User
Tags first string type casting
Copy CodeThe code is as follows:
echo "
numeric coercion: ";
$string = "2a";
$string 1=intval ($string);
Echo ' $string 1 Value: '. $string 1. ' $string 2 value: ';//single quotation mark does not output variable, output as-is
$string 2= (int) ($string);
Echo $string 2
?>


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:
Copy CodeThe code is as follows:
$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:
Copy CodeThe code is as follows:
$str = "10";
$num = (int) $str;
if ($str = = =) echo "String";
if ($num = = =) 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

http://www.bkjia.com/PHPjc/319207.html www.bkjia.com true http://www.bkjia.com/PHPjc/319207.html techarticle Copy the code as follows:? PHP echo "br/value cast:"; $string = "2a"; $string 1=intval ($string); Echo ' $string 1 Value: '. $string 1. ' $string 2 value: ';//single quotation mark does not output ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.