language/types/integer.xml
161dde4fe721309398dd324edbf02aec409f127b
...
...
@@ -4,127 +4,106 @@
4
4
<title>Integers</title>
5
5

6
6
<simpara>
7
-
An <type>integer</type> is a number of the set
7
+
An <type>int</type> is a number of the set
8
8
&#8484; = {..., -2, -1, 0, 1, 2, ...}.
9
9
</simpara>
10
10

11
-
<para>
12
-
See also:
13
-
</para>
14
-

15
-
<itemizedlist>
16
-
<listitem>
17
-
<simpara>
18
-
<link linkend="ref.gmp">Arbitrary length integer / GMP</link>
19
-
</simpara>
20
-
</listitem>
21
-
<listitem>
22
-
<simpara>
23
-
<link linkend="language.types.float">Floating point numbers</link>
24
-
</simpara>
25
-
</listitem>
26
-
<listitem>
27
-
<simpara>
28
-
<link linkend="ref.bc">Arbitrary precision / BCMath</link>
29
-
</simpara>
30
-
</listitem>
31
-
</itemizedlist>
11
+
<sect2 role="seealso">
12
+
&reftitle.seealso;
13
+
<para>
14
+
<simplelist>
15
+
<member><link linkend="language.types.float">Floating point numbers</link></member>
16
+
<member><link linkend="book.bc">Arbitrary precision / BCMath</link></member>
17
+
<member><link linkend="book.gmp">Arbitrary length integer / GMP</link></member>
18
+
</simplelist>
19
+
</para>
20
+
</sect2>
32
21

33
22
<sect2 xml:id="language.types.integer.syntax">
34
23
<title>Syntax</title>
35
24

36
25
<simpara>
37
-
<type>Integer</type>s can be specified in decimal (base 10), hexadecimal
38
-
(base 16), octal (base 8) or binary (base 2) notation, optionally preceded by a sign
39
-
(- or +).
26
+
<type>Int</type>s can be specified in decimal (base 10), hexadecimal
27
+
(base 16), octal (base 8) or binary (base 2) notation.
28
+
The <link linkend="language.operators.arithmetic">negation operator</link>
29
+
can be used to denote a negative <type>int</type>.
40
30
</simpara>
41
31

42
32
<para>
43
-
Binary <type>integer</type> literals are available since PHP 5.4.0.
44
-
</para>
45
-

46
-
<para>
47
33
To use octal notation, precede the number with a <literal>0</literal> (zero).
34
+
As of PHP 8.1.0, octal notation can also be preceded with <literal>0o</literal> or <literal>0O</literal>.
48
35
To use hexadecimal notation precede the number with <literal>0x</literal>.
49
36
To use binary notation precede the number with <literal>0b</literal>.
50
37
</para>
51
38

39
+
<para>
40
+
As of PHP 7.4.0, integer literals may contain underscores (<literal>_</literal>) between digits,
41
+
for better readability of literals. These underscores are removed by PHP's scanner.
42
+
</para>
43
+

52
44
<example>
53
45
<title>Integer literals</title>
54
46
<programlisting role="php">
55
47
<![CDATA[
56
48
<?php
57
49
$a = 1234; // decimal number
58
-
$a = -123; // a negative number
59
50
$a = 0123; // octal number (equivalent to 83 decimal)
51
+
$a = 0o123; // octal number (as of PHP 8.1.0)
60
52
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
61
53
$a = 0b11111111; // binary number (equivalent to 255 decimal)
54
+
$a = 1_234_567; // decimal number (as of PHP 7.4.0)
62
55
?>
63
56
]]>
64
57
</programlisting>
65
58
</example>
66
59

67
60
<para>
68
-
Formally, the structure for <type>integer</type> literals is:
61
+
Formally, the structure for <type>int</type> literals is as of PHP 8.1.0
62
+
(previously, the <literal>0o</literal> or <literal>0O</literal> octal
63
+
prefixes were not allowed, and prior to PHP 7.4.0 the underscores were
64
+
not allowed):
69
65
</para>
70
66

71
67
<informalexample>
72
68
<programlisting>
73
69
<![CDATA[
74
-
decimal : [1-9][0-9]*
70
+
decimal : [1-9][0-9]*(_[0-9]+)*
75
71
| 0
76
72

77
-
hexadecimal : 0[xX][0-9a-fA-F]+
73
+
hexadecimal : 0[xX][0-9a-fA-F]+(_[0-9a-fA-F]+)*
78
74

79
-
octal : 0[0-7]+
75
+
octal : 0[oO]?[0-7]+(_[0-7]+)*
80
76

81
-
binary : 0b[01]+
77
+
binary : 0[bB][01]+(_[01]+)*
82
78

83
-
integer : [+-]?decimal
84
-
| [+-]?hexadecimal
85
-
| [+-]?octal
86
-
| [+-]?binary
79
+
integer : decimal
80
+
| hexadecimal
81
+
| octal
82
+
| binary
87
83
]]>
88
84
</programlisting>
89
85
</informalexample>
90
86

91
87
<para>
92
-
The size of an <type>integer</type> is platform-dependent, although a maximum
88
+
The size of an <type>int</type> is platform-dependent, although a maximum
93
89
value of about two billion is the usual value (that's 32 bits signed).
94
-
64-bit platforms usually have a maximum value of about 9E18, except for
95
-
Windows, which is always 32 bit. PHP does not support unsigned
96
-
<type>integer</type>s. <type>Integer</type> size can be determined using
97
-
the constant <constant>PHP_INT_SIZE</constant>, and maximum value using the
98
-
constant <constant>PHP_INT_MAX</constant> since PHP 4.4.0 and PHP 5.0.5.
90
+
64-bit platforms usually have a maximum value of about 9E18.
91
+
PHP does not support unsigned <type>int</type>s.
92
+
<type>int</type> size can be determined
93
+
using the constant <constant>PHP_INT_SIZE</constant>, maximum value using
94
+
the constant <constant>PHP_INT_MAX</constant>,
95
+
and minimum value using the constant <constant>PHP_INT_MIN</constant>.
99
96
</para>
100
-

101
-
<warning>
102
-
<para>
103
-
If an invalid digit is given in an octal <type>integer</type> (i.e. 8 or 9),
104
-
the rest of the number is ignored.
105
-
</para>
106
-

107
-
<example>
108
-
<title>Octal weirdness</title>
109
-
<programlisting role="php">
110
-
<![CDATA[
111
-
<?php
112
-
var_dump(01090); // 010 octal = 8 decimal
113
-
?>
114
-
]]>
115
-
</programlisting>
116
-
</example>
117
-
</warning>
118
97
</sect2>
119
98

120
99
<sect2 xml:id="language.types.integer.overflow">
121
100
<title>Integer overflow</title>
122
101

123
102
<para>
124
-
If PHP encounters a number beyond the bounds of the <type>integer</type>
103
+
If PHP encounters a number beyond the bounds of the <type>int</type>
125
104
type, it will be interpreted as a <type>float</type> instead. Also, an
126
105
operation which results in a number beyond the bounds of the
127
-
<type>integer</type> type will return a <type>float</type> instead.
106
+
<type>int</type> type will return a <type>float</type> instead.
128
107
</para>
129
108

130
109
<example>
...
...
@@ -166,9 +145,10 @@ var_dump($large_number); // float(5.0E+19)
166
145
</example>
167
146

168
147
<para>
169
-
There is no <type>integer</type> division operator in PHP.
148
+
There is no <type>int</type> division operator in PHP, to achieve this
149
+
use the <function>intdiv</function> function.
170
150
<literal>1/2</literal> yields the <type>float</type> <literal>0.5</literal>.
171
-
The value can be casted to an <type>integer</type> to round it downwards, or
151
+
The value can be cast to an <type>int</type> to round it towards zero, or
172
152
the <function>round</function> function provides finer control over rounding.
173
153
</para>
174
154

...
...
@@ -189,16 +169,16 @@ var_dump(round(25/7)); // float(4)
189
169
<title>Converting to integer</title>
190
170

191
171
<simpara>
192
-
To explicitly convert a value to <type>integer</type>, use either the
172
+
To explicitly convert a value to <type>int</type>, use either the
193
173
<literal>(int)</literal> or <literal>(integer)</literal> casts. However, in
194
174
most cases the cast is not needed, since a value will be automatically
195
175
converted if an operator, function or control structure requires an
196
-
<type>integer</type> argument. A value can also be converted to
197
-
<type>integer</type> with the <function>intval</function> function.
176
+
<type>int</type> argument. A value can also be converted to
177
+
<type>int</type> with the <function>intval</function> function.
198
178
</simpara>
199
179

200
180
<simpara>
201
-
If a <type>resource</type> is converted to an <type>integer</type>, then
181
+
If a <type>resource</type> is converted to an <type>int</type>, then
202
182
the result will be the unique resource number assigned to the
203
183
<type>resource</type> by PHP at runtime.
204
184
</simpara>
...
...
@@ -222,22 +202,47 @@ var_dump(round(25/7)); // float(4)
222
202
</title>
223
203

224
204
<simpara>
225
-
When converting from <type>float</type> to <type>integer</type>, the number
205
+
When converting from <type>float</type> to <type>int</type>, the number
226
206
will be rounded <emphasis>towards zero</emphasis>.
207
+
As of PHP 8.1.0, a deprecation notice is emitted when implicitly converting a non-integral &float; to &integer; which loses precision.
227
208
</simpara>
228
209

210
+
<programlisting role="php">
211
+
<![CDATA[
212
+
<?php
213
+

214
+
function foo($value): int {
215
+
return $value;
216
+
}
217
+

218
+
var_dump(foo(8.1)); // "Deprecated: Implicit conversion from float 8.1 to int loses precision" as of PHP 8.1.0
219
+
var_dump(foo(8.1)); // 8 prior to PHP 8.1.0
220
+
var_dump(foo(8.0)); // 8 in both cases
221
+

222
+
var_dump((int)8.1); // 8 in both cases
223
+
var_dump(intval(8.1)); // 8 in both cases
224
+
?>
225
+
]]>
226
+
</programlisting>
227
+

229
228
<para>
230
-
If the float is beyond the boundaries of <type>integer</type> (usually
229
+
If the float is beyond the boundaries of <type>int</type> (usually
231
230
<literal>+/- 2.15e+9 = 2^31</literal> on 32-bit platforms and
232
-
<literal>+/- 9.22e+18 = 2^63</literal> on 64-bit platforms other than
233
-
Windows), the result is undefined, since the <type>float</type> doesn't
234
-
have enough precision to give an exact <type>integer</type> result. No
235
-
warning, not even a notice will be issued when this happens!
231
+
<literal>+/- 9.22e+18 = 2^63</literal> on 64-bit platforms),
232
+
the result is undefined, since the <type>float</type> doesn't
233
+
have enough precision to give an exact <type>int</type> result.
234
+
No warning, not even a notice will be issued when this happens!
236
235
</para>
237
236

237
+
<note>
238
+
<para>
239
+
NaN and Infinity will always be zero when cast to <type>int</type>.
240
+
</para>
241
+
</note>
242
+

238
243
<warning>
239
244
<para>
240
-
Never cast an unknown fraction to <type>integer</type>, as this can
245
+
Never cast an unknown fraction to <type>int</type>, as this can
241
246
sometimes lead to unexpected results.
242
247
</para>
243
248

...
...
@@ -262,8 +267,19 @@ echo (int) ( (0.1+0.7) * 10 ); // echoes 7!
262
267
<title>From strings</title>
263
268

264
269
<simpara>
265
-
See <link linkend="language.types.string.conversion">String conversion to
266
-
numbers</link>
270
+
If the string is
271
+
<link linkend="language.types.numeric-strings">numeric</link>
272
+
or leading numeric then it will resolve to the
273
+
corresponding integer value, otherwise it is converted to zero
274
+
(<literal>0</literal>).
275
+
</simpara>
276
+
</sect3>
277
+

278
+
<sect3 xml:id="language.types.integer.casting-from-null">
279
+
<title>From <type>NULL</type></title>
280
+

281
+
<simpara>
282
+
&null; is always converted to zero (<literal>0</literal>).
267
283
</simpara>
268
284
</sect3>
269
285

...
...
@@ -272,7 +288,7 @@ echo (int) ( (0.1+0.7) * 10 ); // echoes 7!
272
288

273
289
<caution>
274
290
<simpara>
275
-
The behaviour of converting to <type>integer</type> is undefined for other
291
+
The behaviour of converting to <type>int</type> is undefined for other
276
292
types. Do <emphasis>not</emphasis> rely on any observed behaviour, as it
277
293
can change without notice.
278
294
</simpara>
279
295