Analysis of integer type usage in PHP

Source: Internet
Author: User

Integer can be in decimal, octal, or hexadecimal format.
In octal notation, the number must start with 0 (0;
In hexadecimal notation, the number must begin with 0x (Zero x) or 0X (Zero capital X;

Integer Overflow: When an integer overflows, it is automatically converted to the float type. Similarly, if the integer Operation Result overflows the integer boundary, it is automatically converted to the float type.
Note that the integer type does not have a "/" (Division) operation. To obtain an integer, you can use functions such as round, or directly use (int) or (integer) to convert the data type to integer.

Convert to integer:
Boolean to integer:
1. true is always converted to 1;
2. false is always converted to 0;
Float to integer:
1. If the float type value does not exceed the integer boundary value, the fractional part is directly truncated during conversion.
2. If the float type value exceeds the integer boundary value, the conversion result is not defined, but it is almost certainly not the expected result.
Array type conversion to integer:
1. Empty array is always converted to 0;
2. Non-empty array is always converted to 1;
Object conversion to integer:
1. converting an object type value to an integer will generate Notice information similar to Object of class stdClass cocould not be converted to int. The conversion result is 1;
String to integer:
1. If the string starts with a number and does not contain the characters '.', 'E', and 'E', and the value of the number is within the integer Range, the string is converted to an integer.
Copy codeThe Code is as follows:
$ Resource = fopen ('d:/tmp/test.txt ', 'rb ');
Var_dump ($ resource, (int) $ resource );

2. If the string is a null string or does not start with a number, the string can be converted to integer 0;
3. In other cases, the string type will be converted to the float type;
Resources to integer:
1. to convert the value of the resources type to integer, the corresponding id of the resource will be
For example:

Result
Copy codeThe Code is as follows:
Resource (3) of type (stream) int (3)

Convert null to integer:
1. Converting null to integer is always 0
Undefined variables are converted to integer:
1. If an undefined variable is converted to an integer, a notice information is generated and converted to 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.