language/types/integer.xml
e587d0655e426f97b3fcb431453da5030e743b23
...
...
@@ -8,33 +8,22 @@
8
8
ℤ = {..., -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="book.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="book.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>int</type>s can be specified in decimal (base 10), hexadecimal
26
+
<type>Int</type>s can be specified in decimal (base 10), hexadecimal
38
27
(base 16), octal (base 8) or binary (base 2) notation.
39
28
The <link linkend="language.operators.arithmetic">negation operator</link>
40
29
can be used to denote a negative <type>int</type>.
...
...
@@ -42,6 +31,7 @@
42
31

43
32
<para>
44
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>.
45
35
To use hexadecimal notation precede the number with <literal>0x</literal>.
46
36
To use binary notation precede the number with <literal>0b</literal>.
47
37
</para>
...
...
@@ -53,11 +43,12 @@
53
43

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

69
60
<para>
70
-
Formally, the structure for <type>int</type> literals is as of PHP 7.4.0
71
-
(previously, underscores have not been allowed):
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):
72
65
</para>
73
66

74
67
<informalexample>
...
...
@@ -79,7 +72,7 @@ decimal : [1-9][0-9]*(_[0-9]+)*
79
72

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

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

84
77
binary : 0[bB][01]+(_[01]+)*
85
78

...
...
@@ -114,42 +107,23 @@ integer : decimal
114
107
</para>
115
108

116
109
<example>
117
-
<title>Integer overflow on a 32-bit system</title>
110
+
<title>Integer overflow</title>
118
111
<programlisting role="php">
119
112
<![CDATA[
120
113
<?php
121
-
$large_number = 2147483647;
122
-
var_dump($large_number); // int(2147483647)
123
-

124
-
$large_number = 2147483648;
125
-
var_dump($large_number); // float(2147483648)
114
+
$large_number = 50000000000000000000;
115
+
var_dump($large_number); // float(5.0E+19)
126
116

127
-
$million = 1000000;
128
-
$large_number = 50000 * $million;
129
-
var_dump($large_number); // float(50000000000)
117
+
var_dump(PHP_INT_MAX + 1); // 32-bit system: float(2147483648)
118
+
// 64-bit system: float(9.2233720368548E+18)
130
119
?>
131
120
]]>
132
121
</programlisting>
133
122
</example>
123
+
</sect2>
134
124

135
-
<example>
136
-
<title>Integer overflow on a 64-bit system</title>
137
-
<programlisting role="php">
138
-
<![CDATA[
139
-
<?php
140
-
$large_number = 9223372036854775807;
141
-
var_dump($large_number); // int(9223372036854775807)
142
-

143
-
$large_number = 9223372036854775808;
144
-
var_dump($large_number); // float(9.2233720368548E+18)
145
-

146
-
$million = 1000000;
147
-
$large_number = 50000000000000 * $million;
148
-
var_dump($large_number); // float(5.0E+19)
149
-
?>
150
-
]]>
151
-
</programlisting>
152
-
</example>
125
+
<sect2 xml:id="language.types.integer.division">
126
+
<title>Integer division</title>
153
127

154
128
<para>
155
129
There is no <type>int</type> division operator in PHP, to achieve this
...
...
@@ -159,7 +133,8 @@ var_dump($large_number); // float(5.0E+19)
159
133
the <function>round</function> function provides finer control over rounding.
160
134
</para>
161
135

162
-
<informalexample>
136
+
<example>
137
+
<title>Divisions</title>
163
138
<programlisting role="php">
164
139
<![CDATA[
165
140
<?php
...
...
@@ -169,7 +144,7 @@ var_dump(round(25/7)); // float(4)
169
144
?>
170
145
]]>
171
146
</programlisting>
172
-
</informalexample>
147
+
</example>
173
148
</sect2>
174
149

175
150
<sect2 xml:id="language.types.integer.casting">
...
...
@@ -211,8 +186,30 @@ var_dump(round(25/7)); // float(4)
211
186
<simpara>
212
187
When converting from <type>float</type> to <type>int</type>, the number
213
188
will be rounded <emphasis>towards zero</emphasis>.
189
+
As of PHP 8.1.0, a deprecation notice is emitted when implicitly converting a non-integral &float; to &integer; which loses precision.
214
190
</simpara>
215
191

192
+
<example>
193
+
<title>Casting from Float</title>
194
+
<programlisting role="php">
195
+
<![CDATA[
196
+
<?php
197
+

198
+
function foo($value): int {
199
+
return $value;
200
+
}
201
+

202
+
var_dump(foo(8.1)); // "Deprecated: Implicit conversion from float 8.1 to int loses precision" as of PHP 8.1.0
203
+
var_dump(foo(8.1)); // 8 prior to PHP 8.1.0
204
+
var_dump(foo(8.0)); // 8 in both cases
205
+

206
+
var_dump((int) 8.1); // 8 in both cases
207
+
var_dump(intval(8.1)); // 8 in both cases
208
+
?>
209
+
]]>
210
+
</programlisting>
211
+
</example>
212
+

216
213
<para>
217
214
If the float is beyond the boundaries of <type>int</type> (usually
218
215
<literal>+/- 2.15e+9 = 2^31</literal> on 32-bit platforms and
...
...
@@ -224,7 +221,7 @@ var_dump(round(25/7)); // float(4)
224
221

225
222
<note>
226
223
<para>
227
-
NaN and Infinity will always be zero when cast to <type>int</type>.
224
+
<literal>NaN</literal>, <literal>Inf</literal> and <literal>-Inf</literal> will always be zero when cast to <type>int</type>.
228
225
</para>
229
226
</note>
230
227

231
228