PHP under Intval () and (int) conversion use and Difference

Source: Internet
Author: User
Keywords Intval int
Tags first string type casting
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:

echo "<br/>Numerical conversion:";
echo'$string1 value:'.$string1.'$string2 value:';//Single quotes will not output the variable, it will be output as it is
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 ();
$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:

$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:

$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

Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

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: 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.