<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >布尔型</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="类型" HREF="language.types.html"><LINK REL="PREVIOUS" TITLE="类型" HREF="language.types.html"><LINK REL="NEXT" TITLE="整型" HREF="language.types.integer.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></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 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="language.types.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >章 11. 类型</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.types.integer.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.types.boolean" >布尔型</A ></H1 ><P > 这是最简单的类型。<A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A > 表达了真值,可以为 <TT CLASS="constant" ><B >TRUE</B ></TT > 或 <TT CLASS="constant" ><B >FALSE</B ></TT >。 </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > 布尔类型是 PHP 4 引进的。 </P ></BLOCKQUOTE ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.types.boolean.syntax" >语法</A ></H2 ><P > 要指定一个布尔值,使用关键字 <TT CLASS="constant" ><B >TRUE</B ></TT > 或 <TT CLASS="constant" ><B >FALSE</B ></TT >。两个都不区分大小写。 <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2784" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$foo </font><font color="#007700">= </font><font color="#0000BB">True</font><font color="#007700">; </font><font color="#FF8000">// assign the value TRUE to $foo<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > 通常你用某些<A HREF="language.operators.html" >运算符</A >返回 <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A > 值,并将其传递给<A HREF="language.control-structures.html" >控制流程</A >。 <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2790" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> // == is an operator which test<br />// equality and returns a boolean<br />if ($action == "show_version") {<br /> echo "The version is 1.23";<br />}<br /><br />// this is not necessary...<br />if ($show_separators == TRUE) {<br /> echo "<hr>\n";<br />}<br /><br />// ...because you can simply type<br />if ($show_separators) {<br /> echo "<hr>\n";<br />}</font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.types.boolean.casting" >转换为布尔值</A ></H2 ><P > 要明示地将一个值转换成 <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A >,用 <TT CLASS="literal" >(bool)</TT > 或者 <TT CLASS="literal" >(boolean)</TT > 来强制转换。但是很多情况下不需要用强制转换,因为当运算符,函数或者流程控制需要一个 <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A > 参数时,该值会被自动转换。 </P ><P > 参见<A HREF="language.types.type-juggling.html" >类型戏法</A >。 </P ><P > 当转换为 <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A > 时,以下值被认为是 <TT CLASS="constant" ><B >FALSE</B ></TT >: <P ></P ><UL ><LI ><P ><A HREF="language.types.boolean.html" >布尔</A >值 <TT CLASS="constant" ><B >FALSE</B ></TT ></P ></LI ><LI ><P ><A HREF="language.types.integer.html" >整型</A >值 0(零)</P ></LI ><LI ><P ><A HREF="language.types.float.html" >浮点型</A >值 0.0(零)</P ></LI ><LI ><P > 空白<A HREF="language.types.string.html" >字符串</A >和<A HREF="language.types.string.html" >字符串</A > "0" </P ></LI ><LI ><P >没有成员变量的<A HREF="language.types.array.html" >数组</A ></P ></LI ><LI ><P >没有单元的<A HREF="language.types.object.html" >对象</A >(仅适用于 PHP 4)</P ></LI ><LI ><P >特殊类型 <A HREF="language.types.null.html" >NULL</A >(包括尚未设定的变量)</P ></LI ></UL > 所有其它值都被认为是 <TT CLASS="constant" ><B >TRUE</B ></TT >(包括任何<A HREF="language.types.resource.html" >资源</A >)。 <DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >警告</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > <TT CLASS="literal" >-1</TT > 和其它非零值(不论正负)一样,被认为是 <TT CLASS="constant" ><B >TRUE</B ></TT >! </P ></TD ></TR ></TABLE ></DIV > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2834" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />var_dump</font><font color="#007700">((bool) </font><font color="#DD0000">""</font><font color="#007700">); </font><font color="#FF8000">// bool(false)<br /></font><font color="#0000BB">var_dump</font><font color="#007700">((bool) </font><font color="#0000BB">1</font><font color="#007700">); </font><font color="#FF8000">// bool(true)<br /></font><font color="#0000BB">var_dump</font><font color="#007700">((bool) -</font><font color="#0000BB">2</font><font color="#007700">); </font><font color="#FF8000">// bool(true)<br /></font><font color="#0000BB">var_dump</font><font color="#007700">((bool) </font><font color="#DD0000">"foo"</font><font color="#007700">); </font><font color="#FF8000">// bool(true)<br /></font><font color="#0000BB">var_dump</font><font color="#007700">((bool) </font><font color="#0000BB">2.3e5</font><font color="#007700">); </font><font color="#FF8000">// bool(true)<br /></font><font color="#0000BB">var_dump</font><font color="#007700">((bool) array(</font><font color="#0000BB">12</font><font color="#007700">)); </font><font color="#FF8000">// bool(true)<br /></font><font color="#0000BB">var_dump</font><font color="#007700">((bool) array()); </font><font color="#FF8000">// bool(false)<br /></font><font color="#0000BB">var_dump</font><font color="#007700">((bool) </font><font color="#DD0000">"false"</font><font color="#007700">); </font><font color="#FF8000">// bool(true)<br /></font><font color="#0000BB">?></font> </font> </code></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" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.types.integer.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >类型</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.types.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >整型</TD ></TR ></TABLE ></DIV ></BODY ></HTML >