<!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>Introduction</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.types.html">Types</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.types.boolean.html">Booleans</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.intro" class="sect1"> <h2 class="title">Introduction</h2> <p class="simpara"> PHP supports eight primitive types. </p> <p class="para"> Four scalar types: </p> <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> </span> </li> <li class="listitem"> <span class="simpara"> <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> </span> </li> <li class="listitem"> <span class="simpara"> <span class="type"><a href="language.types.float.html" class="type float">float</a></span> (floating-point number, aka <span class="type"><a href="language.types.float.html" class="type double">double</a></span>) </span> </li> <li class="listitem"> <span class="simpara"> <span class="type"><a href="language.types.string.html" class="type string">string</a></span> </span> </li> </ul> <p class="para"> Two compound types: </p> <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> <span class="type"><a href="language.types.array.html" class="type array">array</a></span> </span> </li> <li class="listitem"> <span class="simpara"> <span class="type"><a href="language.types.object.html" class="type object">object</a></span> </span> </li> </ul> <p class="para"> And finally two special types: </p> <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span> </span> </li> <li class="listitem"> <span class="simpara"> <span class="type"><a href="language.types.null.html" class="type NULL">NULL</a></span> </span> </li> </ul> <p class="para"> This manual also introduces some <a href="language.pseudo-types.html" class="link">pseudo-types</a> for readability reasons: </p> <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> </span> </li> <li class="listitem"> <span class="simpara"> <span class="type"><a href="language.pseudo-types.html#language.types.number" class="type number">number</a></span> </span> </li> <li class="listitem"> <span class="simpara"> <span class="type"><a href="language.pseudo-types.html#language.types.callback" class="type callback">callback</a></span> </span> </li> </ul> <p class="para"> And the pseudo-variable <em><code class="parameter">$...</code></em>. </p> <p class="simpara"> Some references to the type "double" may remain in the manual. Consider double the same as float; the two names exist only for historic reasons. </p> <p class="simpara"> The type of a variable is not usually set by the programmer; rather, it is decided at runtime by PHP depending on the context in which that variable is used. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> To check the type and value of an <a href="language.expressions.html" class="link">expression</a>, use the <span class="function"><a href="function.var-dump.html" class="function">var_dump()</a></span> function. </span> <p class="para"> To get a human-readable representation of a type for debugging, use the <span class="function"><a href="function.gettype.html" class="function">gettype()</a></span> function. To check for a certain type, do <em class="emphasis">not</em> use <span class="function"><a href="function.gettype.html" class="function">gettype()</a></span>, but rather the <em>is_<span class="replaceable">type</span></em> functions. Some examples: </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$a_bool </span><span style="color: #007700">= </span><span style="color: #0000BB">TRUE</span><span style="color: #007700">; </span><span style="color: #FF8000">// a boolean<br /></span><span style="color: #0000BB">$a_str </span><span style="color: #007700">= </span><span style="color: #DD0000">"foo"</span><span style="color: #007700">; </span><span style="color: #FF8000">// a string<br /></span><span style="color: #0000BB">$a_str2 </span><span style="color: #007700">= </span><span style="color: #DD0000">'foo'</span><span style="color: #007700">; </span><span style="color: #FF8000">// a string<br /></span><span style="color: #0000BB">$an_int </span><span style="color: #007700">= </span><span style="color: #0000BB">12</span><span style="color: #007700">; </span><span style="color: #FF8000">// an integer<br /><br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">gettype</span><span style="color: #007700">(</span><span style="color: #0000BB">$a_bool</span><span style="color: #007700">); </span><span style="color: #FF8000">// prints out: boolean<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">gettype</span><span style="color: #007700">(</span><span style="color: #0000BB">$a_str</span><span style="color: #007700">); </span><span style="color: #FF8000">// prints out: string<br /><br />// If this is an integer, increment it by four<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">is_int</span><span style="color: #007700">(</span><span style="color: #0000BB">$an_int</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">$an_int </span><span style="color: #007700">+= </span><span style="color: #0000BB">4</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// If $a_bool is a string, print it out<br />// (does not print out anything)<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">is_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$a_bool</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"String: </span><span style="color: #0000BB">$a_bool</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p></blockquote> <p class="simpara"> To forcibly convert a variable to a certain type, either <a href="language.types.type-juggling.html#language.types.typecasting" class="link">cast</a> the variable or use the <span class="function"><a href="function.settype.html" class="function">settype()</a></span> function on it. </p> <p class="simpara"> Note that a variable may be evaluated with different values in certain situations, depending on what type it is at the time. For more information, see the section on <a href="language.types.type-juggling.html" class="link">Type Juggling</a>. <a href="types.comparisons.html" class="link">The type comparison tables</a> may also be useful, as they show examples of various type-related comparisons. </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.types.html">Types</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.types.boolean.html">Booleans</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>