<HTML ><HEAD ><TITLE >Types</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Language Reference" HREF="langref.html"><LINK REL="PREVIOUS" TITLE="Comments" HREF="language.basic-syntax.comments.html"><LINK REL="NEXT" TITLE="Booleans" HREF="language.types.boolean.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="chapter" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="language.basic-syntax.comments.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.types.boolean.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="chapter" ><H1 ><A NAME="language.types" >Chapter 7. Types</A ></H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Table of Contents</B ></DT ><DT ><A HREF="language.types.html#language.types.intro" >Introduction</A ></DT ><DT ><A HREF="language.types.boolean.html" >Booleans</A ></DT ><DT ><A HREF="language.types.integer.html" >Integers</A ></DT ><DT ><A HREF="language.types.float.html" >Floating point numbers</A ></DT ><DT ><A HREF="language.types.string.html" >Strings</A ></DT ><DT ><A HREF="language.types.array.html" >Arrays</A ></DT ><DT ><A HREF="language.types.object.html" >Objects</A ></DT ><DT ><A HREF="language.types.resource.html" >Resource</A ></DT ><DT ><A HREF="language.types.null.html" >NULL</A ></DT ><DT ><A HREF="language.pseudo-types.html" >Pseudo-types used in this documentation</A ></DT ><DT ><A HREF="language.types.type-juggling.html" >Type Juggling</A ></DT ></DL ></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.types.intro" ></A >Introduction</H1 ><P > PHP supports eight primitive types. </P ><P > Four scalar types: <P ></P ><UL ><LI ><P > <A HREF="language.types.boolean.html" >boolean</A > </P ></LI ><LI ><P > <A HREF="language.types.integer.html" >integer</A > </P ></LI ><LI ><P > <A HREF="language.types.float.html" >floating-point number (float)</A > </P ></LI ><LI ><P > <A HREF="language.types.string.html" >string</A > </P ></LI ></UL > Two compound types: <P ></P ><UL ><LI ><P > <A HREF="language.types.array.html" >array</A > </P ></LI ><LI ><P > <A HREF="language.types.object.html" >object</A > </P ></LI ></UL > And finally two special types: <P ></P ><UL ><LI ><P > <A HREF="language.types.resource.html" >resource</A > </P ></LI ><LI ><P > <A HREF="language.types.null.html" >NULL</A > </P ></LI ></UL > This manual also introduces some <A HREF="language.pseudo-types.html" >pseudo-types</A > for readability reasons: <P ></P ><UL ><LI ><P > <A HREF="language.pseudo-types.html#language.types.mixed" >mixed</A > </P ></LI ><LI ><P > <A HREF="language.pseudo-types.html#language.types.number" >number</A > </P ></LI ><LI ><P > <A HREF="language.pseudo-types.html#language.types.mixed" >callback</A > </P ></LI ></UL > You may also find some references to the type "double". Consider double the same as float, the two names exist only for historic reasons. </P ><P > The type of a variable is usually not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which that variable is used. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > If you want to check out the type and value of a certain <A HREF="language.expressions.html" >expression</A >, use <A HREF="function.var-dump.html" ><B CLASS="function" >var_dump()</B ></A >. </P ><P ><B >Note: </B > If you simply want a human-readable representation of the type for debugging, use <A HREF="function.gettype.html" ><B CLASS="function" >gettype()</B ></A >. To check for a certain type, do <SPAN CLASS="emphasis" ><I CLASS="emphasis" >not</I ></SPAN > use <A HREF="function.gettype.html" ><B CLASS="function" >gettype()</B ></A >, but use the <TT CLASS="literal" >is_<TT CLASS="replaceable" ><I >type</I ></TT ></TT > functions. Some examples: <DIV CLASS="informalexample" ><A NAME="AEN3217" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $bool = TRUE; // a boolean $str = "foo"; // a string $int = 12; // an integer echo gettype($bool); // prints out "boolean" echo gettype($str); // prints out "string" // If this is an integer, increment it by four if (is_int($int)) { $int += 4; } // If $bool is a string, print it out // (does not print out anything) if (is_string($bool)) { echo "String: $bool"; } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></BLOCKQUOTE ></DIV ><P > If you would like to force a variable to be converted to a certain type, you may either <A HREF="language.types.type-juggling.html#language.types.typecasting" >cast</A > the variable or use the <A HREF="function.settype.html" ><B CLASS="function" >settype()</B ></A > function on it. </P ><P > 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" >Type Juggling</A >. </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="language.basic-syntax.comments.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.types.boolean.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Comments</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="langref.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Booleans</TD ></TR ></TABLE ></DIV ></BODY ></HTML >