<HTML ><HEAD ><TITLE >Booleans</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="Types" HREF="language.types.html"><LINK REL="PREVIOUS" TITLE="Types" HREF="language.types.html"><LINK REL="NEXT" TITLE="Integers" HREF="language.types.integer.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" 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.types.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 7. Types</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.types.integer.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.types.boolean" ></A >Booleans</H1 ><P > This is the easiest type. A <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A > expresses a truth value. It can be either <TT CLASS="constant" ><B >TRUE</B ></TT > or <TT CLASS="constant" ><B >FALSE</B ></TT >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > The boolean type was introduced in PHP 4. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.types.boolean.syntax" ></A >Syntax</H2 ><P > To specify a boolean literal, use either the keyword <TT CLASS="constant" ><B >TRUE</B ></TT > or <TT CLASS="constant" ><B >FALSE</B ></TT >. Both are case-insensitive. <DIV CLASS="informalexample" ><A NAME="AEN3237" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $foo = True; // assign the value TRUE to $foo ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > Usually you use some kind of <A HREF="language.operators.html" >operator</A > which returns a <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A > value, and then pass it on to a <A HREF="control-structures.html" >control structure</A >. <DIV CLASS="informalexample" ><A NAME="AEN3243" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // == is an operator which test // equality and returns a boolean if ($action == "show_version") { echo "The version is 1.23"; } // this is not necessary... if ($show_separators == TRUE) { echo "<hr>\n"; } // ...because you can simply type if ($show_separators) { echo "<hr>\n"; } ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.types.boolean.casting" ></A >Converting to boolean</H2 ><P > To explicitly convert a value to <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A >, use either the <TT CLASS="literal" >(bool)</TT > or the <TT CLASS="literal" >(boolean)</TT > cast. However, in most cases you do not need to use the cast, since a value will be automatically converted if an operator, function or control structure requires a <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A > argument. </P ><P > See also <A HREF="language.types.type-juggling.html" >Type Juggling</A >. </P ><P > When converting to <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A >, the following values are considered <TT CLASS="constant" ><B >FALSE</B ></TT >: <P ></P ><UL ><LI ><P >the <A HREF="language.types.boolean.html" >boolean</A > <TT CLASS="constant" ><B >FALSE</B ></TT > itself</P ></LI ><LI ><P >the <A HREF="language.types.integer.html" >integer</A > 0 (zero) </P ></LI ><LI ><P >the <A HREF="language.types.float.html" >float</A > 0.0 (zero) </P ></LI ><LI ><P >the empty <A HREF="language.types.string.html" >string</A >, and the <A HREF="language.types.string.html" >string</A > "0"</P ></LI ><LI ><P >an <A HREF="language.types.array.html" >array</A > with zero elements</P ></LI ><LI ><P >an <A HREF="language.types.object.html" >object</A > with zero member variables</P ></LI ><LI ><P >the special type <A HREF="language.types.null.html" >NULL</A > (including unset variables) </P ></LI ></UL > Every other value is considered <TT CLASS="constant" ><B >TRUE</B ></TT > (including any <A HREF="language.types.resource.html" >resource</A >). <DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warning</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > <TT CLASS="literal" >-1</TT > is considered <TT CLASS="constant" ><B >TRUE</B ></TT >, like any other non-zero (whether negative or positive) number! </P ></TD ></TR ></TABLE ></DIV > <DIV CLASS="informalexample" ><A NAME="AEN3287" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php echo gettype((bool) ""); // bool(false) echo gettype((bool) 1); // bool(true) echo gettype((bool) -2); // bool(true) echo gettype((bool) "foo"); // bool(true) echo gettype((bool) 2.3e5); // bool(true) echo gettype((bool) array(12)); // bool(true) echo gettype((bool) array()); // bool(false) ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </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.types.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.integer.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Types</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.types.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Integers</TD ></TR ></TABLE ></DIV ></BODY ></HTML >