1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 297557 $ -->
<sect1 xml:id="language.types.type-juggling">
<title>Type Juggling</title>
<simpara>
PHP does not require (or support) explicit type definition in variable
declaration; a variable's type is determined by the context in which the
variable is used. That is to say, if a <type>string</type> value is assigned
to variable <varname>$var</varname>, <varname>$var</varname> becomes a
<type>string</type>. If an <type>integer</type> value is then assigned to
<varname>$var</varname>, it becomes an <type>integer</type>.
</simpara>
<para>
An example of PHP's automatic type conversion is the addition operator '+'.
If either operand is a <type>float</type>, then both operands are evaluated as
<type>float</type>s, and the result will be a <type>float</type>. Otherwise,
the operands will be interpreted as <type>integer</type>s, and the result will
also be an <type>integer</type>. Note that this does <emphasis>not</emphasis>
change the types of the operands themselves; the only change is in how the
operands are evaluated and what the type of the expression itself is.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$foo = "0"; // $foo is string (ASCII 48)
$foo += 2; // $foo is now an integer (2)
$foo = $foo + 1.3; // $foo is now a float (3.3)
$foo = 5 + "10 Little Piggies"; // $foo is integer (15)
$foo = 5 + "10 Small Pigs"; // $foo is integer (15)
?>
]]>
<!-- bad example, no real operator (must be used with variable, modifies it too)
$foo++; // $foo is the string "1" (ASCII 49)
TODO: explain ++/- - behaviour with strings
examples:
++'001' = '002'
++'abc' = 'abd'
++'xyz' = 'xza'
++'9.9' = '9.0'
++'-3' = '-4'
- -'9' = 8 (integer!)
- -'5.5' = '5.5'
- -'-9' = -10 (integer)
- -'09' = 8 (integer)
- -'abc' = 'abc'
-->
</programlisting>
</informalexample>
<simpara>
If the last two examples above seem odd, see
<link linkend="language.types.string.conversion">String conversion to
numbers</link>.
</simpara>
<simpara>
To force a variable to be evaluated as a certain type, see the section on
<link linkend="language.types.typecasting">Type casting</link>. To change the
type of a variable, see the <function>settype</function> function.
</simpara>
<para>
To test any of the examples in this section, use the
<function>var_dump</function> function.
</para>
<note>
<para>
The behaviour of an automatic conversion to <type>array</type> is currently
undefined.
</para>
<para>
Also, because PHP supports indexing into <type>string</type>s via offsets
using the same syntax as <type>array</type> indexing, the following example
holds true for all PHP versions:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$a = 'car'; // $a is a string
$a[0] = 'b'; // $a is still a string
echo $a; // bar
?>
]]>
</programlisting>
</informalexample>
<para>
See the section titled <link linkend="language.types.string.substr">String
access by character</link> for more information.
</para>
</note>
<sect2 xml:id="language.types.typecasting">
<title>Type Casting</title>
<para>
Type casting in PHP works much as it does in C: the name of the desired type
is written in parentheses before the variable which is to be cast.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$foo = 10; // $foo is an integer
$bar = (boolean) $foo; // $bar is a boolean
?>
]]>
</programlisting>
</informalexample>
<para>
The casts allowed are:
</para>
<itemizedlist>
<listitem>
<simpara>(int), (integer) - cast to <type>integer</type></simpara>
</listitem>
<listitem>
<simpara>(bool), (boolean) - cast to <type>boolean</type></simpara>
</listitem>
<listitem>
<simpara>(float), (double), (real) - cast to <type>float</type></simpara>
</listitem>
<listitem>
<simpara>(string) - cast to <type>string</type></simpara>
</listitem>
<!-- FIXME PHP_6
<listitem>
<simpara>(binary) - cast to binary <type>string</type> (PHP 6)</simpara>
</listitem>
-->
<listitem>
<simpara>(array) - cast to <type>array</type></simpara>
</listitem>
<listitem>
<simpara>(object) - cast to <type>object</type></simpara>
</listitem>
<listitem>
<simpara>(unset) - cast to <type>NULL</type> (PHP 5)</simpara>
</listitem>
</itemizedlist>
<para>
(binary) casting and b prefix forward support was added in PHP 5.2.1
</para>
<para>
Note that tabs and spaces are allowed inside the parentheses, so the
following are functionally equivalent:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$foo = (int) $bar;
$foo = ( int ) $bar;
?>
]]>
</programlisting>
<para>
Casting literal <type>string</type>s and variables to binary
<type>string</type>s:
</para>
<programlisting role="php">
<![CDATA[
<?php
$binary = (binary) $string;
$binary = b"binary string";
?>
]]>
</programlisting>
</informalexample>
<note>
<para>
Instead of casting a variable to a <type>string</type>, it is also possible
to enclose the variable in double quotes.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$foo = 10; // $foo is an integer
$str = "$foo"; // $str is a string
$fst = (string) $foo; // $fst is also a string
// This prints out that "they are the same"
if ($fst === $str) {
echo "they are the same";
}
?>
]]>
</programlisting>
</informalexample>
</note>
<para>
It may not be obvious exactly what will happen when casting between certain
types. For more information, see these sections:
</para>
<itemizedlist>
<listitem>
<simpara>
<link linkend="language.types.boolean.casting">Converting to boolean</link>
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="language.types.integer.casting">Converting to integer</link>
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="language.types.float.casting">Converting to float</link>
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="language.types.string.casting">Converting to string</link>
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="language.types.array.casting">Converting to array</link>
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="language.types.object.casting">Converting to object</link>
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="language.types.resource.casting">Converting to
resource</link>
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="language.types.null.casting">Converting to NULL</link>
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="types.comparisons">The type comparison tables</link>
</simpara>
</listitem>
</itemizedlist>
</sect2>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|