|
5 | 5 |
|
6 | 6 | <sect1 xml:id="language.types.intro">
|
7 | 7 | <title>Introduction</title>
|
8 |
| - |
9 |
| - <simpara> |
10 |
| - PHP supports ten primitive types. |
11 |
| - </simpara> |
12 |
| - |
| 8 | + |
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) |
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> |
| 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> |
96 | 51 |
|
97 |
| - <simpara> |
98 |
| - The type of a variable is not usually set by the programmer; rather, it is |
99 |
| - decided at runtime by PHP depending on the context in which that variable is |
100 |
| - used. |
101 |
| - </simpara> |
102 |
| - |
103 | 52 | <note>
|
104 | 53 | <simpara>
|
105 |
| - To check the type and value of an |
106 |
| - <link linkend="language.expressions">expression</link>, use the |
107 |
| - <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. |
108 | 58 | </simpara>
|
| 59 | + </note> |
| 60 | + |
| 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. |
109 | 71 |
|
110 |
| - <para> |
111 |
| - To get a human-readable representation of a type for debugging, use the |
112 |
| - <function>gettype</function> function. To check for a certain type, do |
113 |
| - <emphasis>not</emphasis> use <function>gettype</function>, but rather the |
114 |
| - <literal>is_<replaceable>type</replaceable></literal> functions. Some |
115 |
| - examples: |
116 |
| - </para> |
117 |
| - |
118 | 72 | <informalexample>
|
119 | 73 | <programlisting role="php">
|
120 | 74 | <![CDATA[
|
121 | 75 | <?php
|
122 |
| -$a_bool = TRUE; // a boolean |
| 76 | +$a_bool = true; // a bool |
123 | 77 | $a_str = "foo"; // a string
|
124 | 78 | $a_str2 = 'foo'; // a string
|
125 |
| -$an_int = 12; // an integer |
| 79 | +$an_int = 12; // an int |
126 | 80 |
|
127 |
| -echo gettype($a_bool); // prints out: boolean |
128 |
| -echo gettype($a_str); // prints out: string |
| 81 | +echo get_debug_type($a_bool), "\n"; |
| 82 | +echo get_debug_type($a_str), "\n"; |
129 | 83 |
|
130 | 84 | // If this is an integer, increment it by four
|
131 | 85 | if (is_int($an_int)) {
|
132 | 86 | $an_int += 4;
|
133 | 87 | }
|
| 88 | +var_dump($an_int); |
134 | 89 |
|
135 | 90 | // If $a_bool is a string, print it out
|
136 |
| -// (does not print out anything) |
137 | 91 | if (is_string($a_bool)) {
|
138 | 92 | echo "String: $a_bool";
|
139 | 93 | }
|
140 | 94 | ?>
|
141 | 95 | ]]>
|
142 | 96 | </programlisting>
|
| 97 | + &example.outputs.8; |
| 98 | + <screen> |
| 99 | +<![CDATA[ |
| 100 | +bool |
| 101 | +string |
| 102 | +int(16) |
| 103 | +]]> |
| 104 | + </screen> |
143 | 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> |
144 | 113 | </note>
|
145 |
| - |
146 |
| - <simpara> |
147 |
| - To forcibly convert a variable to a certain type, either |
148 |
| - <link linkend="language.types.typecasting">cast</link> the variable or use |
149 |
| - the <function>settype</function> function on it. |
150 |
| - </simpara> |
151 |
| - |
152 |
| - <simpara> |
153 |
| - Note that a variable may be evaluated with different values in certain |
154 |
| - situations, depending on what type it is at the time. For more information, |
155 |
| - see the section on <link linkend="language.types.type-juggling">Type |
156 |
| - Juggling</link>. <link linkend="types.comparisons">The type comparison |
157 |
| - tables</link> may also be useful, as they show examples of various |
158 |
| - type-related comparisons. |
159 |
| - </simpara> |
160 | 114 | </sect1>
|
161 |
| - |
| 115 | + |
| 116 | + &language.types.type-system; |
| 117 | + &language.types.null; |
162 | 118 | &language.types.boolean;
|
163 | 119 | &language.types.integer;
|
164 | 120 | &language.types.float;
|
165 | 121 | &language.types.string;
|
166 | 122 | &language.types.numeric-strings;
|
167 | 123 | &language.types.array;
|
168 |
| - &language.types.iterable; |
169 | 124 | &language.types.object;
|
170 | 125 | &language.types.enumerations;
|
171 | 126 | &language.types.resource;
|
172 |
| - &language.types.null; |
173 | 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.literal; |
| 133 | + &language.types.iterable; |
174 | 134 | &language.types.declarations;
|
175 | 135 | &language.types.type-juggling;
|
176 |
| - |
| 136 | + |
177 | 137 | </chapter>
|
178 |
| - |
179 | 138 | <!-- Keep this comment at the end of the file
|
180 | 139 | Local variables:
|
181 | 140 | mode: sgml
|
|
0 commit comments