<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = 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',
    2 => 'Introduction',
  ),
  'up' => 
  array (
    0 => 'language.types.php',
    1 => 'Types',
  ),
  'prev' => 
  array (
    0 => 'language.types.php',
    1 => 'Types',
  ),
  'next' => 
  array (
    0 => 'language.types.type-system.php',
    1 => 'Type System',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/types.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.types.intro" class="sect1">
  <h2 class="title">Introduction</h2>

  <p class="para">
   Every single expression in PHP has one of the following
   built-in types depending on its value:
   <ul class="itemizedlist">
    <li class="listitem"><span class="simpara"><span class="type"><a href="language.types.null.php" class="type null">null</a></span></span></li>
    <li class="listitem"><span class="simpara"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></span></li>
    <li class="listitem"><span class="simpara"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span></span></li>
    <li class="listitem"><span class="simpara"><span class="type"><a href="language.types.float.php" class="type float">float</a></span> (floating-point number)</span></li>
    <li class="listitem"><span class="simpara"><span class="type"><a href="language.types.string.php" class="type string">string</a></span></span></li>
    <li class="listitem"><span class="simpara"><span class="type"><a href="language.types.array.php" class="type array">array</a></span></span></li>
    <li class="listitem"><span class="simpara"><span class="type"><a href="language.types.object.php" class="type object">object</a></span></span></li>
    <li class="listitem"><span class="simpara"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span></span></li>
    <li class="listitem"><span class="simpara"><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span></span></li>
   </ul>
  </p>

  <p class="para">
   PHP is a dynamically typed language, which means that by default there is
   no need to specify the type of a variable, as this will be determined at
   runtime. However, it is possible to statically type some aspect of the
   language via the use of
   <a href="language.types.declarations.php" class="link">type declarations</a>.
   Different types that are supported by PHP&#039;s type system can be found at the
   <a href="language.types.type-system.php" class="link">type system</a> page.
  </p>

  <p class="para">
   Types restrict the kind of operations that can be performed on them.
   However, if an expression/variable is used in an operation which
   its type does not support, PHP will attempt to
   <a href="language.types.type-juggling.php" class="link">type juggle</a>
   the value into a type that supports the operation.
   This process depends on the context in which the value is used.
   For more information, see the section on
   <a href="language.types.type-juggling.php" class="link">Type Juggling</a>.
  </p>

  <div class="tip"><strong class="tip">Tip</strong>
   <p class="simpara">
    <a href="types.comparisons.php" class="link">The type comparison tables</a>
    may also be useful, as various examples of comparison between values of
    different types are present.
   </p>
  </div>
  
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    It is possible to force an expression to be evaluated to a certain type by
    using a <a href="language.types.type-juggling.php#language.types.typecasting" class="link">type cast</a>.
    A variable can also be type cast in-place by using the
    <span class="function"><a href="function.settype.php" class="function">settype()</a></span> function on it.
   </span>
  </p></blockquote>

  <p class="para">
   To check the value and type of an
   <a href="language.expressions.php" class="link">expression</a>,
   use the <span class="function"><a href="function.var-dump.php" class="function">var_dump()</a></span> function.
   To retrieve the type of an
   <a href="language.expressions.php" class="link">expression</a>,
   use the <span class="function"><a href="function.get-debug-type.php" class="function">get_debug_type()</a></span> function.
   However, to check if an expression is of a certain type use the
   
   <code class="literal">is_<span class="replaceable">type</span></code> functions instead.

   <div class="example" id="example-1">
    <p><strong>Example #1 Different Types</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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 bool<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 int<br /><br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">get_debug_type</span><span style="color: #007700">(</span><span style="color: #0000BB">$a_bool</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">get_debug_type</span><span style="color: #007700">(</span><span style="color: #0000BB">$a_str</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// 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 /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$an_int</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// If $a_bool is a string, print it out<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">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Output of the above example in PHP 8:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
bool
string
int(16)
</pre></div>
    </div>
   </div>
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    Prior to PHP 8.0.0, where the <span class="function"><a href="function.get-debug-type.php" class="function">get_debug_type()</a></span> is not
    available, the <span class="function"><a href="function.gettype.php" class="function">gettype()</a></span> function can be used instead.
    However, it doesn&#039;t use the canonical type names.
   </span>
  </p></blockquote>
 </div><?php manual_footer($setup); ?>