downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

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 &quot;double&quot; 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">&lt;?php<br />$a_bool&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;a&nbsp;boolean<br /></span><span style="color: #0000BB">$a_str&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;a&nbsp;string<br /></span><span style="color: #0000BB">$a_str2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;a&nbsp;string<br /></span><span style="color: #0000BB">$an_int&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">12</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;an&nbsp;integer<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">gettype</span><span style="color: #007700">(</span><span style="color: #0000BB">$a_bool</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;prints&nbsp;out:&nbsp;&nbsp;boolean<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">gettype</span><span style="color: #007700">(</span><span style="color: #0000BB">$a_str</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;prints&nbsp;out:&nbsp;&nbsp;string<br /><br />//&nbsp;If&nbsp;this&nbsp;is&nbsp;an&nbsp;integer,&nbsp;increment&nbsp;it&nbsp;by&nbsp;four<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">is_int</span><span style="color: #007700">(</span><span style="color: #0000BB">$an_int</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$an_int&nbsp;</span><span style="color: #007700">+=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;If&nbsp;$bool&nbsp;is&nbsp;a&nbsp;string,&nbsp;print&nbsp;it&nbsp;out<br />//&nbsp;(does&nbsp;not&nbsp;print&nbsp;out&nbsp;anything)<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">is_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$a_bool</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"String:&nbsp;</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><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(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites