In PHP, int is an integer.
An integer is a set of Z = {...,-2,-1, 0,
A number in 1, 2.
The integer value can be expressed in decimal, hexadecimal, or octal notation. An optional Symbol (-or +) can be added before ).
The octal value indicates that the value must be added before the number.0
(0), which must be added before a number in hexadecimal notation0x
.
Example #1 integer text expression
<? PHP$
=
1234
;
// Decimal number
$
=-
123
;
// Negative
$
=
0123
;
// Octal digits (equal to 83 in decimal format)
$
=
0x1a
;
// Hexadecimal number (equal to or greater than 26 in decimal format)
?>
Integer)
Format description:
decimal : [1-9][0-9]*
| 0
hexadecimal : 0[xX][0-9a-fA-F]+
octal : 0[0-7]+
integer : [+-]?decimal
| [+-]?hexadecimal
| [+-]?octal
The length of the integer is related to the platform, although the maximum value is usually about 2 billion (32-Bit Signed ). PHP does not support unsigned integers. Integer
The character length of a value can be a constant.Php_int_size
From
After PHP 4.4.0 and PHP 5.0.5, the maximum value can be a constant.Php_int_max
.
Note:
If an invalid number (8 or 9) is passed to the octal number, the remaining digits are ignored.
Example #2 strange things about the eight-digit number
<? PHPVar_dump
(
01090
);
// Octal 010 = decimal 8
?>
Integer Overflow
If the given number exceeds integer
Will be interpreted as float
. Similarly, if the execution result exceeds integer
Range, float will also be returned
.
<? PHP$ Large_number
=
2147483647
;
Var_dump
(
$ Large_number
);
// Output: int (2147483647)
$ Large_number
=
2147483648
;
Var_dump
(
$ Large_number
);
// The output is float (2147483648)
// It also applies to the hexadecimal integers from 2 ^ 31 to 2 ^ 32-1:
Var_dump
(
0 xffffffff
);
// Output: Float (4294967295)
// It is not applicable to the numbers in hexadecimal notation greater than 2 ^ 32-1:
Var_dump
(
Zero X 100000000
);
// Output: int (2147483647)
$ Million
=
1000000
;
$ Large_number
=
50000
*
$ Million
;
Var_dump
(
$ Large_number
);
// Output: Float (50000000000)
?>
To explicitly convert a value to an integer
, Use(INT)
Or
(Integer)
Forced conversion. However, in most cases, no forced conversion is required, because when an operator, function, or flow control requires an integer
The value is automatically converted. You can also use the intval () function ()
To convert a value to an integer.
When converting from a floating point to an integerToward Zero
Integer.
If the floating point number exceeds the Integer Range (usually+/-2.15e + 9 =
2 ^ 31
), The result is uncertain, because there is not enough precision to make the floating point number give an exact integer result. In this case, there is no warning or even no notification!
Warning
Never forcibly convert an unknown score to an integer
, Which sometimes leads to unexpected results.
<? PHP
Echo (INT )((
0.1
+
0.7
)*
10
);
// Display 7!
?>
Pay attention to integer overflow. When a positive number overflows, it is converted to the float type. When the secondary overflow occurs, different operations are performed based on the operating system.
For example:
<? PHP
Echo
(INT )-
3000000000
;
// A 32bit negative Overflow
?>
In Windows and Ubuntu, the result is
1294967296, but-2147483648 in FreeBSD.
Be careful when using it. When Using Integer Conversion to test some evaluations, check whether it is a positive integer or not. You may get unexpected behavior.
<? PHP
Error_reporting
(
E_all
);
Require_once
'Date. php'
;
$ Date
= New
Date
();
Print
"/$ Date is an instance"
.
Get_class
(
$ Date
).
"/N"
;
$ Date
+ =
0
;
Print
"/$ Date is now
$ Date
/N"
;
Var_dump
(
$ Date
);
$ Foo
= New
Foo
();
Print
"/$ Foo is an instance"
.
Get_class
(
$ Foo
).
"/N"
;
$ Foo
+ =
0
;
Print
"/$ Foo is now
$ Foo
/N"
;
Var_dump
(
$ Foo
);
Class
Foo
{
VaR
$ Bar
=
0
;
VaR
$ Baz
=
"La Lal la"
;
VaR
$ Bak
;
Function
Foo
(){
$ Bak
=
3.14159
;
}
}
?>
Output result:
$ Date is an instance of date
Notice: object of class date cocould not be
Converted to int in/home/kpeters/work/sketches/objectsketch. php on line
7
$ Date is now 1
INT (1)
$ Foo is an instance of foo
Notice:
Object of class Foo cocould not be converted to int in
/Home/kpeters/work/sketches/objectsketch. php on line 13
$ Foo is now
1
INT (1)
This is because the object is first converted to a Boolean value of true, and converted to integer 1 When 0 is added.