Differences between intval () and (INT) Conversion in PHP

Source: Internet
Author: User

CopyCode The Code is as follows: <? PHP
Echo "<br/> numerical value forced conversion :";
$ String = "2a ";
$ String1 = intval ($ string );
Echo '$ string1 value:'. $ string1. '$ string2 value:'; // The variable is not output in single quotes and is output as is.
$ String2 = (INT) ($ string );
Echo $ string2
?>

The Manual cannot be found.
This is also the reference in the manual:
Int intval (mixed $ var [, int $ base])
Return the integer value of the variable VAR by using a specific hexadecimal conversion (decimal by default. If there is only this difference, then I like to use (INT) to process the 10-digit system. Is it a good choice?
There is no difference, generally (INT), and float, String, array, etc.

For intval (), if the parameter is a string, the integer represented by the first numeric string that is not a digit before the string is returned. If the first string is '-', it starts from the second string.

If the parameter is a number of characters, return the value after the value is rounded up.

Of course, the value returned by intval () is within the range (-2147483648 ~ 2147483647). The value that exceeds this range will be replaced by the boundary value.

Example: intval ("A") = 0; intval (12.3223) = 12; intval ("1123asdfka3243") = 1123;
INT ();
Example:
$ 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 Conversion to int

Sometimes, it is important to have a variable value in int format. Eaxmple, if your visitor fills in the form with age fields, this should be an int. However, in the $ _ post array, you use it as a string.
It is easy to convert a PHP string to an int. Before we use it, you need to use (INT) Your variable type casting. So ). The following is an example of how to do this:Copy codeThe Code is as follows: <? PHP
$ STR = "10 ";
$ Num = (INT) $ STR;?>

If you want to check the code realy project, we can use the = Operator. This operator checks not only values, but types as well. The code looks like this:Copy codeThe Code is as follows: <? PHP
$ STR = "10 ";
$ Num = (INT) $ STR;
If ($ STR = 10) echo "string ";
If ($ num = 10) echo "integer ";
?>

Another problem is openness. What happens if our string is not a string of numbers. I mean there are characters in other strings. In this case, the conversion operation tries the best and convertible strings, if only space is available, if there is no valid character after the numeric value. It works as follows:

"10"-> 10
"10.5"-> 10
"10, 5"-> 10
"10"-> 10
"10"-> 10
"10test"-> 10
"Test10"-> 0

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.