language/types.xml
5fccbe5195820cd9ec0045674312ec567ef5f6ed
...
...
@@ -6,181 +6,135 @@
6
6
<sect1 xml:id="language.types.intro">
7
7
<title>Introduction</title>
8
8

9
-
<simpara>
10
-
PHP supports ten primitive types.
11
-
</simpara>
12
-
13
9
<para>
14
-
Four scalar types:
10
+
Every single expression in PHP has one of the following
11
+
built-in types depending on its value:
12
+
<itemizedlist>
13
+
<listitem><simpara><type>null</type></simpara></listitem>
14
+
<listitem><simpara><type>bool</type></simpara></listitem>
15
+
<listitem><simpara><type>int</type></simpara></listitem>
16
+
<listitem><simpara><type>float</type> (floating-point number)</simpara></listitem>
17
+
<listitem><simpara><type>string</type></simpara></listitem>
18
+
<listitem><simpara><type>array</type></simpara></listitem>
19
+
<listitem><simpara><type>object</type></simpara></listitem>
20
+
<listitem><simpara><type>callable</type></simpara></listitem>
21
+
<listitem><simpara><type>resource</type></simpara></listitem>
22
+
</itemizedlist>
15
23
</para>
16
24

17
-
<itemizedlist>
18
-

19
-
<listitem>
20
-
<simpara>
21
-
<type>bool</type>
22
-
</simpara>
23
-
</listitem>
24
-

25
-
<listitem>
26
-
<simpara>
27
-
<type>int</type>
28
-
</simpara>
29
-
</listitem>
30
-

31
-
<listitem>
32
-
<simpara>
33
-
<type>float</type> (floating-point number, aka <type>double</type>)
34
-
</simpara>
35
-
</listitem>
36
-

37
-
<listitem>
38
-
<simpara>
39
-
<type>string</type>
40
-
</simpara>
41
-
</listitem>
42
-

43
-
</itemizedlist>
44
-

45
25
<para>
46
-
Four compound types:
26
+
PHP is a dynamically typed language, which means that by default there is
27
+
no need to specify the type of a variable, as this will be determined at
28
+
runtime. However, it is possible to statically type some aspect of the
29
+
language via the use of
30
+
<link linkend="language.types.declarations">type declarations</link>.
47
31
</para>
48
32

49
-
<itemizedlist>
50
-

51
-
<listitem>
52
-
<simpara>
53
-
<type>array</type>
54
-
</simpara>
55
-
</listitem>
56
-

57
-
<listitem>
58
-
<simpara>
59
-
<type>object</type>
60
-
</simpara>
61
-
</listitem>
62
-

63
-
<listitem>
64
-
<simpara>
65
-
<type>callable</type>
66
-
</simpara>
67
-
</listitem>
68
-

69
-
<listitem>
70
-
<simpara>
71
-
<type>iterable</type>
72
-
</simpara>
73
-
</listitem>
74
-

75
-
</itemizedlist>
76
-

77
33
<para>
78
-
And finally two special types:
34
+
Types restrict the kind of operations that can be performed on them.
35
+
However, if an expression/variable is used in an operation which
36
+
its type does not support, PHP will attempt to
37
+
<link linkend="language.types.type-juggling">type juggle</link>
38
+
the value into a type that supports the operation.
39
+
This process depends on the context in which the value is used.
40
+
For more information, see the section on
41
+
<link linkend="language.types.type-juggling">Type Juggling</link>.
79
42
</para>
80
43

81
-
<itemizedlist>
82
-

83
-
<listitem>
84
-
<simpara>
85
-
<type>resource</type>
86
-
</simpara>
87
-
</listitem>
88
-

89
-
<listitem>
90
-
<simpara>
91
-
<type>NULL</type>
92
-
</simpara>
93
-
</listitem>
94
-

95
-
</itemizedlist>
96
-

97
-
<simpara>
98
-
Some references to the type "double" may remain in the manual. Consider
99
-
double the same as float; the two names exist only for historic reasons.
100
-
</simpara>
101
-
102
-
<simpara>
103
-
The type of a variable is not usually set by the programmer; rather, it is
104
-
decided at runtime by PHP depending on the context in which that variable is
105
-
used.
106
-
</simpara>
44
+
<tip>
45
+
<simpara>
46
+
<link linkend="types.comparisons">The type comparison tables</link>
47
+
may also be useful, as various examples of comparison between values of
48
+
different types are present.
49
+
</simpara>
50
+
</tip>
107
51
108
52
<note>
109
53
<simpara>
110
-
To check the type and value of an
111
-
<link linkend="language.expressions">expression</link>, use the
112
-
<function>var_dump</function> function.
54
+
It is possible to force an expression to be evaluated to a certain type by
55
+
using a <link linkend="language.types.typecasting">type cast</link>.
56
+
A variable can also be type cast in-place by using the
57
+
<function>settype</function> function on it.
113
58
</simpara>
59
+
</note>
114
60

115
-
<para>
116
-
To get a human-readable representation of a type for debugging, use the
117
-
<function>gettype</function> function. To check for a certain type, do
118
-
<emphasis>not</emphasis> use <function>gettype</function>, but rather the
119
-
<literal>is_<replaceable>type</replaceable></literal> functions. Some
120
-
examples:
121
-
</para>
61
+
<para>
62
+
To check the value and type of an
63
+
<link linkend="language.expressions">expression</link>,
64
+
use the <function>var_dump</function> function.
65
+
To retrieve the type of an
66
+
<link linkend="language.expressions">expression</link>,
67
+
use the <function>get_debug_type</function> function.
68
+
However, to check if an expression is of a certain type use the
69
+
<!-- TODO When PhD support is there: <function>is_<replaceable>type</replaceable></function> -->
70
+
<literal>is_<replaceable>type</replaceable></literal> functions instead.
122
71

123
72
<informalexample>
124
73
<programlisting role="php">
125
74
<![CDATA[
126
75
<?php
127
-
$a_bool = TRUE; // a boolean
76
+
$a_bool = true; // a bool
128
77
$a_str = "foo"; // a string
129
78
$a_str2 = 'foo'; // a string
130
-
$an_int = 12; // an integer
79
+
$an_int = 12; // an int
131
80

132
-
echo gettype($a_bool); // prints out: boolean
133
-
echo gettype($a_str); // prints out: string
81
+
echo get_debug_type($a_bool), "\n";
82
+
echo get_debug_type($a_str), "\n";
134
83

135
84
// If this is an integer, increment it by four
136
85
if (is_int($an_int)) {
137
86
$an_int += 4;
138
87
}
88
+
var_dump($an_int);
139
89

140
90
// If $a_bool is a string, print it out
141
-
// (does not print out anything)
142
91
if (is_string($a_bool)) {
143
92
echo "String: $a_bool";
144
93
}
145
94
?>
146
95
]]>
147
96
</programlisting>
97
+
&example.outputs.8;
98
+
<screen>
99
+
<![CDATA[
100
+
bool
101
+
string
102
+
int(16)
103
+
]]>
104
+
</screen>
148
105
</informalexample>
106
+
</para>
107
+
<note>
108
+
<simpara>
109
+
Prior to PHP 8.0.0, where the <function>get_debug_type</function> is not
110
+
available, the <function>gettype</function> function can be used instead.
111
+
However, it doesn't use the canonical type names.
112
+
</simpara>
149
113
</note>
150
-

151
-
<simpara>
152
-
To forcibly convert a variable to a certain type, either
153
-
<link linkend="language.types.typecasting">cast</link> the variable or use
154
-
the <function>settype</function> function on it.
155
-
</simpara>
156
-

157
-
<simpara>
158
-
Note that a variable may be evaluated with different values in certain
159
-
situations, depending on what type it is at the time. For more information,
160
-
see the section on <link linkend="language.types.type-juggling">Type
161
-
Juggling</link>. <link linkend="types.comparisons">The type comparison
162
-
tables</link> may also be useful, as they show examples of various
163
-
type-related comparisons.
164
-
</simpara>
165
114
</sect1>
166
115

116
+
&language.types.type-system;
117
+
&language.types.null;
167
118
&language.types.boolean;
168
119
&language.types.integer;
169
120
&language.types.float;
170
121
&language.types.string;
171
122
&language.types.numeric-strings;
172
123
&language.types.array;
173
-
&language.types.iterable;
174
124
&language.types.object;
175
125
&language.types.enumerations;
176
126
&language.types.resource;
177
-
&language.types.null;
178
127
&language.types.callable;
128
+
&language.types.mixed;
129
+
&language.types.void;
130
+
&language.types.never;
131
+
&language.types.relative-class-types;
132
+
&language.types.value;
133
+
&language.types.iterable;
179
134
&language.types.declarations;
180
135
&language.types.type-juggling;
181
136

182
137
</chapter>
183
-
184
138
<!-- Keep this comment at the end of the file
185
139
Local variables:
186
140
mode: sgml
187
141