language/types/integer.xml
161dde4fe721309398dd324edbf02aec409f127b
...
...
@@ -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>
...
...
@@ -58,6 +48,7 @@
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

...
...
@@ -211,8 +204,27 @@ var_dump(round(25/7)); // float(4)
211
204
<simpara>
212
205
When converting from <type>float</type> to <type>int</type>, the number
213
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.
214
208
</simpara>
215
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
+

216
228
<para>
217
229
If the float is beyond the boundaries of <type>int</type> (usually
218
230
<literal>+/- 2.15e+9 = 2^31</literal> on 32-bit platforms and
219
231