Integer type usage analysis in PHP

  • 2020-03-31 21:01:49
  • OfStack

An integer can be represented in decimal, octal, or hexadecimal.
In octal notation, Numbers need to start with 0 (zero);
In hexadecimal notation, Numbers need to begin with 0x(zero x) or 0x(zero capital x);

Integer overflow: when an integer overflows, it is automatically converted to float. Similarly, if the result of an operation of type integer overflows the bounds of type integer, it is automatically converted to float.
One thing to note is that integer types do not have "/" (division) operations, and if you need to get an integer you can use functions such as round, or you can use (int) or (integer) to force the type to be integer.

Convert to integer:
Boolean to integer:
1. True always converts to 1;
2. False is always converted to 0;
Float to integer:
1. If the float value does not exceed the integer boundary value, the decimal part will be truncated during the conversion.
2. If the float value exceeds the integer boundary value, the result of the conversion is undefined, but almost certainly not the expected result.
Array type to integer:
1. An empty array is always converted to 0;
2. Non-empty array is always converted to 1;
Convert object to integer:
1, the value of the type of the object is converted to an integer can be the object of like class stdClass could not be converted to int Notice information. The result of conversion is 1;
String to integer:
1. If string begins with a number and does not contain the characters'.','e',' e', and the value of the number is in the integer range, string will be converted to an integer.
 
$resource = fopen('d:/tmp/test.txt', 'rb'); 
var_dump($resource, (int)$resource); 

2. If string is an empty string or does not start with a number, string can be converted to integer 0;
3. In other cases, string will be converted to float;
Convert resources to integer:
1. Convert a value of type resources to an integer, and the corresponding resource id will be converted
Such as:

The results of the
 
resource(3) of type (stream) int(3) 

Null to integer:
1. Convert null to integer always 0
Undefined variable converted to integer:
1. Converting an undefined variable to integer produces a notice message and converts to 0

Related articles: