<!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="langref.html"><LINK REL="PREVIOUS" TITLE="注释" HREF="language.basic-syntax.comments.html"><LINK REL="NEXT" TITLE="布尔型" HREF="language.types.boolean.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></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 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="language.basic-syntax.comments.html" ACCESSKEY="P" >上一页</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" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="chapter" ><H1 ><A NAME="language.types" >章 11. 类型</A ></H1 ><DIV CLASS="TOC" ><DL ><DT ><B >目录</B ></DT ><DT ><A HREF="language.types.html#language.types.intro" >介绍</A ></DT ><DT ><A HREF="language.types.boolean.html" >布尔型</A ></DT ><DT ><A HREF="language.types.integer.html" >整型</A ></DT ><DT ><A HREF="language.types.float.html" >浮点型</A ></DT ><DT ><A HREF="language.types.string.html" >字符串</A ></DT ><DT ><A HREF="language.types.array.html" >数组</A ></DT ><DT ><A HREF="language.types.object.html" >对象</A ></DT ><DT ><A HREF="language.types.resource.html" >资源</A ></DT ><DT ><A HREF="language.types.null.html" >NULL</A ></DT ><DT ><A HREF="language.pseudo-types.html" >本文档中使用的伪类型</A ></DT ><DT ><A HREF="language.types.type-juggling.html" >类型戏法</A ></DT ></DL ></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.types.intro" >介绍</A ></H1 ><P > PHP 支持八种原始类型。 </P ><P > 四种标量类型: <P ></P ><UL ><LI ><P > <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A >(布尔型) </P ></LI ><LI ><P > <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A >(整型) </P ></LI ><LI ><P > <A HREF="language.types.float.html" ><B CLASS="type" >float</B ></A >(浮点型,也作“<A HREF="language.types.float.html" ><B CLASS="type" >double</B ></A >”) </P ></LI ><LI ><P > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A >(字符串) </P ></LI ></UL > 两种复合类型: <P ></P ><UL ><LI ><P > <A HREF="language.types.array.html" ><B CLASS="type" >array</B ></A >(数组) </P ></LI ><LI ><P > <A HREF="language.types.object.html" ><B CLASS="type" >object</B ></A >(对象) </P ></LI ></UL > 最后是两种特殊类型: <P ></P ><UL ><LI ><P > <A HREF="language.types.resource.html" ><B CLASS="type" >resource</B ></A >(资源) </P ></LI ><LI ><P > <A HREF="language.types.null.html" ><B CLASS="type" >NULL</B ></A > </P ></LI ></UL > 为了确保代码的易读性,本手册还介绍了一些<A HREF="language.pseudo-types.html" >伪类型</A >: <P ></P ><UL ><LI ><P > <A HREF="language.pseudo-types.html#language.types.mixed" ><B CLASS="type" >mixed</B ></A > </P ></LI ><LI ><P > <A HREF="language.pseudo-types.html#language.types.number" ><B CLASS="type" >number</B ></A > </P ></LI ><LI ><P > <A HREF="language.pseudo-types.html#language.types.callback" ><B CLASS="type" >callback</B ></A > </P ></LI ></UL > 可能还会读到一些关于“双精度(double)”类型的参考。实际上 double 和 float 是相同的,由于一些历史的原因,这两个名称同时存在。 </P ><P > 变量的类型通常不是由程序员设定的,确切地说,是由 PHP 根据该变量使用的上下文在运行时决定的。 </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > 如果想查看某个<A HREF="language.expressions.html" >表达式</A >的值和类型,用 <A HREF="function.var-dump.html" ><B CLASS="function" >var_dump()</B ></A >。 </P ><P ><B >注意: </B > 如果只是想得到一个易读懂的类型的表达方式用于调试,用 <A HREF="function.gettype.html" ><B CLASS="function" >gettype()</B ></A >。要查看某个类型,<SPAN CLASS="emphasis" ><I CLASS="emphasis" >不要</I ></SPAN >用 <A HREF="function.gettype.html" ><B CLASS="function" >gettype()</B ></A >,而用 <TT CLASS="literal" >is_<TT CLASS="replaceable" ><I >type</I ></TT ></TT > 函数。以下是一些范例: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN2763" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$bool </font><font color="#007700">= </font><font color="#0000BB">TRUE</font><font color="#007700">; </font><font color="#FF8000">// a boolean<br /></font><font color="#0000BB">$str </font><font color="#007700">= </font><font color="#DD0000">"foo"</font><font color="#007700">; </font><font color="#FF8000">// a string<br /></font><font color="#0000BB">$int </font><font color="#007700">= </font><font color="#0000BB">12</font><font color="#007700">; </font><font color="#FF8000">// an integer<br /><br /></font><font color="#007700">echo </font><font color="#0000BB">gettype</font><font color="#007700">(</font><font color="#0000BB">$bool</font><font color="#007700">); </font><font color="#FF8000">// prints out "boolean"<br /></font><font color="#007700">echo </font><font color="#0000BB">gettype</font><font color="#007700">(</font><font color="#0000BB">$str</font><font color="#007700">); </font><font color="#FF8000">// prints out "string"<br /><br />// If this is an integer, increment it by four<br /></font><font color="#007700">if (</font><font color="#0000BB">is_int</font><font color="#007700">(</font><font color="#0000BB">$int</font><font color="#007700">)) {<br /> </font><font color="#0000BB">$int </font><font color="#007700">+= </font><font color="#0000BB">4</font><font color="#007700">;<br />}<br /><br /></font><font color="#FF8000">// If $bool is a string, print it out<br />// (does not print out anything)<br /></font><font color="#007700">if (</font><font color="#0000BB">is_string</font><font color="#007700">(</font><font color="#0000BB">$bool</font><font color="#007700">)) {<br /> echo </font><font color="#DD0000">"String: $bool"</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </P ></BLOCKQUOTE ></DIV ><P > 如果要将一个变量强制转换为某类型,可以对其使用<A HREF="language.types.type-juggling.html#language.types.typecasting" >强制转换</A >或者 <A HREF="function.settype.html" ><B CLASS="function" >settype()</B ></A > 函数。 </P ><P > 注意变量根据其当时的类型在特定场合下会表现出不同的值。更多信息见<A HREF="language.types.type-juggling.html" >类型戏法</A >。此外,你还可以参考 <A HREF="types.comparisons.html" >PHP 类型比较表</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" >上一页</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.boolean.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >注释</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="langref.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >布尔型</TD ></TR ></TABLE ></DIV ></BODY ></HTML >