Source of: /manual/en/language.types.intro.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.types.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'language.types.intro.php',
1 => 'Introduction',
),
'up' =>
array (
0 => 'language.types.php',
1 => 'Types',
),
'prev' =>
array (
0 => 'language.types.php',
1 => 'Types',
),
'next' =>
array (
0 => 'language.types.boolean.php',
1 => 'Booleans',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<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">
<a href="language.types.boolean.php" class="type boolean">boolean</a>
</span>
</li>
<li class="listitem">
<span class="simpara">
<a href="language.types.integer.php" class="type integer">integer</a>
</span>
</li>
<li class="listitem">
<span class="simpara">
<a href="language.types.float.php" class="type float">float</a> (floating-point number, aka <a href="language.types.float.php" class="type double">double</a>)
</span>
</li>
<li class="listitem">
<span class="simpara">
<a href="language.types.string.php" class="type string">string</a>
</span>
</li>
</ul>
<p class="para">
Two compound types:
</p>
<ul class="itemizedlist">
<li class="listitem">
<span class="simpara">
<a href="language.types.array.php" class="type array">array</a>
</span>
</li>
<li class="listitem">
<span class="simpara">
<a href="language.types.object.php" class="type object">object</a>
</span>
</li>
</ul>
<p class="para">
And finally two special types:
</p>
<ul class="itemizedlist">
<li class="listitem">
<span class="simpara">
<a href="language.types.resource.php" class="type resource">resource</a>
</span>
</li>
<li class="listitem">
<span class="simpara">
<a href="language.types.null.php" class="type NULL">NULL</a>
</span>
</li>
</ul>
<p class="para">
This manual also introduces some
<a href="language.pseudo-types.php" class="link">pseudo-types</a> for readability
reasons:
</p>
<ul class="itemizedlist">
<li class="listitem">
<span class="simpara">
<a href="language.pseudo-types.php#language.types.mixed" class="type mixed">mixed</a>
</span>
</li>
<li class="listitem">
<span class="simpara">
<a href="language.pseudo-types.php#language.types.number" class="type number">number</a>
</span>
</li>
<li class="listitem">
<span class="simpara">
<a href="language.pseudo-types.php#language.types.callback" class="type callback">callback</a>
</span>
</li>
</ul>
<p class="para">
And the pseudo-variable <i><tt class="parameter">$...</tt></i>
.
</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><p><b class="note">Note</b>:
<span class="simpara">
To check the type and value of an
<a href="language.expressions.php" class="link">expression</a>, use the
<a href="function.var-dump.php" class="function">var_dump()</a> function.
</span>
To get a human-readable representation of a type for debugging, use the
<a href="function.gettype.php" class="function">gettype()</a> function. To check for a certain type, do
<em class="emphasis">not</em> use <a href="function.gettype.php" class="function">gettype()</a>, but rather the
<i>is_<span class="replaceable">type</span></i> functions. Some
examples:
<br />
</p><div class="informalexample">
<div class="example-contents programlisting">
<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 $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>
</p></blockquote>
<p class="simpara">
To forcibly convert a variable to a certain type, either
<a href="language.types.type-juggling.php#language.types.typecasting" class="link">cast</a> the variable or use
the <a href="function.settype.php" class="function">settype()</a> 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.php" class="link">Type
Juggling</a>. <a href="types.comparisons.php" class="link">The type comparison
tables</a> may also be useful, as they show examples of various
type-related comparisons.
</p>
</div><?php manual_footer(); ?>