<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Type Juggling</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.pseudo-types.html">Pseudo-types and variables used in this documentation</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.variables.html">Variables</a></div> <div class="up"><a href="language.types.html">Types</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="language.types.type-juggling" class="sect1"> <h2 class="title">Type Juggling</h2> <p class="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 <span class="type"><a href="language.types.string.html" class="type string">string</a></span> value is assigned to variable <var class="varname"><var class="varname">$var</var></var>, <var class="varname"><var class="varname">$var</var></var> becomes a <span class="type"><a href="language.types.string.html" class="type string">string</a></span>. If an <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> value is then assigned to <var class="varname"><var class="varname">$var</var></var>, it becomes an <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>. </p> <p class="para"> An example of PHP's automatic type conversion is the addition operator '+'. If either operand is a <span class="type"><a href="language.types.float.html" class="type float">float</a></span>, then both operands are evaluated as <span class="type"><a href="language.types.float.html" class="type float">float</a></span>s, and the result will be a <span class="type"><a href="language.types.float.html" class="type float">float</a></span>. Otherwise, the operands will be interpreted as <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>s, and the result will also be an <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>. Note that this does <em class="emphasis">not</em> 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. </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$foo </span><span style="color: #007700">= </span><span style="color: #DD0000">"0"</span><span style="color: #007700">; </span><span style="color: #FF8000">// $foo is string (ASCII 48)<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">+= </span><span style="color: #0000BB">2</span><span style="color: #007700">; </span><span style="color: #FF8000">// $foo is now an integer (2)<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">$foo </span><span style="color: #007700">+ </span><span style="color: #0000BB">1.3</span><span style="color: #007700">; </span><span style="color: #FF8000">// $foo is now a float (3.3)<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">5 </span><span style="color: #007700">+ </span><span style="color: #DD0000">"10 Little Piggies"</span><span style="color: #007700">; </span><span style="color: #FF8000">// $foo is integer (15)<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">5 </span><span style="color: #007700">+ </span><span style="color: #DD0000">"10 Small Pigs"</span><span style="color: #007700">; </span><span style="color: #FF8000">// $foo is integer (15)<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <p class="simpara"> If the last two examples above seem odd, see <a href="language.types.string.html#language.types.string.conversion" class="link">String conversion to numbers</a>. </p> <p class="simpara"> To force a variable to be evaluated as a certain type, see the section on <a href="language.types.type-juggling.html#language.types.typecasting" class="link">Type casting</a>. To change the type of a variable, see the <span class="function"><a href="function.settype.html" class="function">settype()</a></span> function. </p> <p class="para"> To test any of the examples in this section, use the <span class="function"><a href="function.var-dump.html" class="function">var_dump()</a></span> function. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The behaviour of an automatic conversion to <span class="type"><a href="language.types.array.html" class="type array">array</a></span> is currently undefined. </p> <p class="para"> Also, because PHP supports indexing into <span class="type"><a href="language.types.string.html" class="type string">string</a></span>s via offsets using the same syntax as <span class="type"><a href="language.types.array.html" class="type array">array</a></span> indexing, the following example holds true for all PHP versions: </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$a </span><span style="color: #007700">= </span><span style="color: #DD0000">'car'</span><span style="color: #007700">; </span><span style="color: #FF8000">// $a is a string<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">] = </span><span style="color: #DD0000">'b'</span><span style="color: #007700">; </span><span style="color: #FF8000">// $a is still a string<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$a</span><span style="color: #007700">; </span><span style="color: #FF8000">// bar<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <p class="para"> See the section titled <a href="language.types.string.html#language.types.string.substr" class="link">String access by character</a> for more information. </p> </p></blockquote> <div class="sect2" id="language.types.typecasting"> <h3 class="title">Type Casting</h3> <p class="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. </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">10</span><span style="color: #007700">; </span><span style="color: #FF8000">// $foo is an integer<br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= (boolean) </span><span style="color: #0000BB">$foo</span><span style="color: #007700">; </span><span style="color: #FF8000">// $bar is a boolean<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <p class="para"> The casts allowed are: </p> <ul class="itemizedlist"> <li class="listitem"> <span class="simpara">(int), (integer) - cast to <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span></span> </li> <li class="listitem"> <span class="simpara">(bool), (boolean) - cast to <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span></span> </li> <li class="listitem"> <span class="simpara">(float), (double), (real) - cast to <span class="type"><a href="language.types.float.html" class="type float">float</a></span></span> </li> <li class="listitem"> <span class="simpara">(string) - cast to <span class="type"><a href="language.types.string.html" class="type string">string</a></span></span> </li> <li class="listitem"> <span class="simpara">(array) - cast to <span class="type"><a href="language.types.array.html" class="type array">array</a></span></span> </li> <li class="listitem"> <span class="simpara">(object) - cast to <span class="type"><a href="language.types.object.html" class="type object">object</a></span></span> </li> <li class="listitem"> <span class="simpara">(unset) - cast to <span class="type"><a href="language.types.null.html" class="type NULL">NULL</a></span> (PHP 5)</span> </li> </ul> <p class="para"> (binary) casting and b prefix forward support was added in PHP 5.2.1 </p> <p class="para"> Note that tabs and spaces are allowed inside the parentheses, so the following are functionally equivalent: </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$foo </span><span style="color: #007700">= (int) </span><span style="color: #0000BB">$bar</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= ( int ) </span><span style="color: #0000BB">$bar</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <p class="para"> Casting literal <span class="type"><a href="language.types.string.html" class="type string">string</a></span>s and variables to binary <span class="type"><a href="language.types.string.html" class="type string">string</a></span>s: </p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$binary </span><span style="color: #007700">= (binary) </span><span style="color: #0000BB">$string</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$binary </span><span style="color: #007700">= </span><span style="color: #DD0000">b"binary string"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Instead of casting a variable to a <span class="type"><a href="language.types.string.html" class="type string">string</a></span>, it is also possible to enclose the variable in double quotes. </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">10</span><span style="color: #007700">; </span><span style="color: #FF8000">// $foo is an integer<br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$foo</span><span style="color: #DD0000">"</span><span style="color: #007700">; </span><span style="color: #FF8000">// $str is a string<br /></span><span style="color: #0000BB">$fst </span><span style="color: #007700">= (string) </span><span style="color: #0000BB">$foo</span><span style="color: #007700">; </span><span style="color: #FF8000">// $fst is also a string<br /><br />// This prints out that "they are the same"<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$fst </span><span style="color: #007700">=== </span><span style="color: #0000BB">$str</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"they are the same"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p></blockquote> <p class="para"> It may not be obvious exactly what will happen when casting between certain types. For more information, see these sections: </p> <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> <a href="language.types.boolean.html#language.types.boolean.casting" class="link">Converting to boolean</a> </span> </li> <li class="listitem"> <span class="simpara"> <a href="language.types.integer.html#language.types.integer.casting" class="link">Converting to integer</a> </span> </li> <li class="listitem"> <span class="simpara"> <a href="language.types.float.html#language.types.float.casting" class="link">Converting to float</a> </span> </li> <li class="listitem"> <span class="simpara"> <a href="language.types.string.html#language.types.string.casting" class="link">Converting to string</a> </span> </li> <li class="listitem"> <span class="simpara"> <a href="language.types.array.html#language.types.array.casting" class="link">Converting to array</a> </span> </li> <li class="listitem"> <span class="simpara"> <a href="language.types.object.html#language.types.object.casting" class="link">Converting to object</a> </span> </li> <li class="listitem"> <span class="simpara"> <a href="language.types.resource.html#language.types.resource.casting" class="link">Converting to resource</a> </span> </li> <li class="listitem"> <span class="simpara"> <a href="language.types.null.html#language.types.null.casting" class="link">Converting to NULL</a> </span> </li> <li class="listitem"> <span class="simpara"> <a href="types.comparisons.html" class="link">The type comparison tables</a> </span> </li> </ul> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.pseudo-types.html">Pseudo-types and variables used in this documentation</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.variables.html">Variables</a></div> <div class="up"><a href="language.types.html">Types</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>