Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > 05cd670d8a02b2b4a0ffb1756f2e8308 > files > 12001

php-manual-zh-5.2.4-1mdv2008.1.noarch.rpm

<!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.expressions.html"><LINK
REL="NEXT"
TITLE="算术运算符"
HREF="language.operators.arithmetic.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.expressions.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.operators.arithmetic.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="chapter"
><H1
><A
NAME="language.operators"
>章 15. 运算符</A
></H1
><DIV
CLASS="TOC"
><DL
><DT
><B
>目录</B
></DT
><DT
><A
HREF="language.operators.html#language.operators.precedence"
>运算符优先级</A
></DT
><DT
><A
HREF="language.operators.arithmetic.html"
>算术运算符</A
></DT
><DT
><A
HREF="language.operators.assignment.html"
>赋值运算符</A
></DT
><DT
><A
HREF="language.operators.bitwise.html"
>位运算符</A
></DT
><DT
><A
HREF="language.operators.comparison.html"
>比较运算符</A
></DT
><DT
><A
HREF="language.operators.errorcontrol.html"
>错误控制运算符</A
></DT
><DT
><A
HREF="language.operators.execution.html"
>执行运算符</A
></DT
><DT
><A
HREF="language.operators.increment.html"
>递增/递减运算符</A
></DT
><DT
><A
HREF="language.operators.logical.html"
>逻辑运算符</A
></DT
><DT
><A
HREF="language.operators.string.html"
>字符串运算符</A
></DT
><DT
><A
HREF="language.operators.array.html"
>数组运算符</A
></DT
><DT
><A
HREF="language.operators.type.html"
>类型运算符</A
></DT
></DL
></DIV
><P
>&#13;   运算符是可以通过给出的一或多个值(用编程行话来说,表达式)来产生另一个值(因而整个结构成为一个表达式)的东西。所以可以认为函数或任何会返回一个值(例如
   print)的结构是运算符,而那些没有返回值的(例如 echo)是别的东西。
  </P
><P
>&#13;   有三种类型的运算符。第一种是一元运算符,只运算一个值,例如
   !(取反运算符)或 ++(加一运算符)。第二种是有限二元运算符,PHP
   支持的大多数运算符都是这种。列表见下节<A
HREF="language.operators.html#language.operators.precedence"
>运算符优先级</A
>。
  </P
><P
>&#13;   第三种是三元运算符:?:。它应该被用来根据一个表达式在另两个表达式中选择一个,而不是用来在两个语句或者程序路线中选择。把整个三元表达式放在扩号里是个很好的主意。
  </P
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="language.operators.precedence"
>运算符优先级</A
></H1
><P
>&#13;    运算符优先级指定了两个表达式绑定得有多“紧密”。例如,表达式 <TT
CLASS="literal"
>1 + 5 *
    3</TT
> 的结果是 <TT
CLASS="literal"
>16</TT
> 而不是 <TT
CLASS="literal"
>18</TT
>
    是因为乘号(“*”)的优先级比加号(“+”)高。必要时可以用括号来强制改变优先级。例如:<TT
CLASS="literal"
>(1
    + 5) * 3</TT
> 的值为 <TT
CLASS="literal"
>18</TT
>。如果运算符优先级相同,则使用从左到右的左联顺序。
   </P
><P
>&#13;    下表从高到低列出了运算符的优先级。同一行中的运算符具有相同优先级,此时它们的结合方向决定求值顺序。
    <DIV
CLASS="table"
><A
NAME="AEN4051"
></A
><P
><B
>表 15-1. 运算符优先级</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><THEAD
><TR
><TH
>结合方向</TH
><TH
>运算符</TH
><TH
>附加信息</TH
></TR
></THEAD
><TBODY
><TR
><TD
>非结合</TD
><TD
>new</TD
><TD
><A
HREF="language.oop5.basic.html#language.oop5.basic.new"
>new</A
></TD
></TR
><TR
><TD
>左</TD
><TD
>[</TD
><TD
><A
HREF="function.array.html"
><B
CLASS="function"
>array()</B
></A
></TD
></TR
><TR
><TD
>非结合</TD
><TD
>++ --</TD
><TD
>&#13;         <A
HREF="language.operators.increment.html"
>递增/递减运算符</A
>
        </TD
></TR
><TR
><TD
>非结合</TD
><TD
>! ~ - (int) (float) (string) (array) (object) @</TD
><TD
>&#13;         <A
HREF="language.types.html"
>类型</A
>
        </TD
></TR
><TR
><TD
>左</TD
><TD
>* / %</TD
><TD
>&#13;         <A
HREF="language.operators.arithmetic.html"
>算数运算符</A
>
        </TD
></TR
><TR
><TD
>左</TD
><TD
>+ - .</TD
><TD
>&#13;         <A
HREF="language.operators.arithmetic.html"
>算数运算符</A
>和<A
HREF="language.operators.string.html"
>字符串运算符</A
></TD
></TR
><TR
><TD
>左</TD
><TD
>&#60;&#60; &#62;&#62;</TD
><TD
>&#13;         <A
HREF="language.operators.bitwise.html"
>位运算符</A
>
        </TD
></TR
><TR
><TD
>非结合</TD
><TD
>&#60; &#60;= &#62; &#62;=</TD
><TD
>&#13;         <A
HREF="language.operators.comparison.html"
>比较运算符</A
>
        </TD
></TR
><TR
><TD
>非结合</TD
><TD
>== != === !==</TD
><TD
>&#13;         <A
HREF="language.operators.comparison.html"
>比较运算符</A
>
        </TD
></TR
><TR
><TD
>左</TD
><TD
>&#38;</TD
><TD
>&#13;         <A
HREF="language.operators.bitwise.html"
>位运算符</A
>和<A
HREF="language.references.html"
>引用</A
></TD
></TR
><TR
><TD
>左</TD
><TD
>^</TD
><TD
>&#13;         <A
HREF="language.operators.bitwise.html"
>位运算符</A
>
        </TD
></TR
><TR
><TD
>左</TD
><TD
>|</TD
><TD
>&#13;         <A
HREF="language.operators.bitwise.html"
>位运算符</A
>
        </TD
></TR
><TR
><TD
>左</TD
><TD
>&#38;&#38;</TD
><TD
>&#13;         <A
HREF="language.operators.logical.html"
>逻辑运算符</A
>
        </TD
></TR
><TR
><TD
>左</TD
><TD
>||</TD
><TD
>&#13;         <A
HREF="language.operators.logical.html"
>逻辑运算符</A
>
        </TD
></TR
><TR
><TD
>左</TD
><TD
>? :</TD
><TD
>&#13;         <A
HREF="language.operators.comparison.html#language.operators.comparison.ternary"
>三元运算符</A
>
        </TD
></TR
><TR
><TD
>右</TD
><TD
>&#13;         = += -= *= /= .= %= &#38;= |= ^= &#60;&#60;= &#62;&#62;=
        </TD
><TD
>&#13;         <A
HREF="language.operators.assignment.html"
>赋值运算符</A
>
        </TD
></TR
><TR
><TD
>左</TD
><TD
>and</TD
><TD
>&#13;         <A
HREF="language.operators.logical.html"
>逻辑运算符</A
>
        </TD
></TR
><TR
><TD
>左</TD
><TD
>xor</TD
><TD
>&#13;         <A
HREF="language.operators.logical.html"
>逻辑运算符</A
>
        </TD
></TR
><TR
><TD
>左</TD
><TD
>or</TD
><TD
>&#13;         <A
HREF="language.operators.logical.html"
>逻辑运算符</A
>
        </TD
></TR
><TR
><TD
>左</TD
><TD
>,</TD
><TD
>多处用到</TD
></TR
></TBODY
></TABLE
></DIV
>
   </P
><P
>&#13;    左联表示表达式从左向右求值,右联相反。
    <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN4162"
></A
><P
><B
>例 15-1. 结合方向</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$a </font><font color="#007700">= </font><font color="#0000BB">3 </font><font color="#007700">* </font><font color="#0000BB">3 </font><font color="#007700">% </font><font color="#0000BB">5</font><font color="#007700">; </font><font color="#FF8000">// (3 * 3) % 5 = 4<br /></font><font color="#0000BB">$a </font><font color="#007700">= </font><font color="#0000BB">true </font><font color="#007700">? </font><font color="#0000BB">0 </font><font color="#007700">: </font><font color="#0000BB">true </font><font color="#007700">? </font><font color="#0000BB">1 </font><font color="#007700">: </font><font color="#0000BB">2</font><font color="#007700">; </font><font color="#FF8000">// (true ? 0 : true) ? 1 : 2 = 2<br /><br /></font><font color="#0000BB">$a </font><font color="#007700">= </font><font color="#0000BB">1</font><font color="#007700">;<br /></font><font color="#0000BB">$b </font><font color="#007700">= </font><font color="#0000BB">2</font><font color="#007700">;<br /></font><font color="#0000BB">$a </font><font color="#007700">= </font><font color="#0000BB">$b </font><font color="#007700">+= </font><font color="#0000BB">3</font><font color="#007700">; </font><font color="#FF8000">// $a = ($b += 3) -&gt; $a = 5, $b = 5<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    使用括号可以增强代码的可读性。
   </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
     尽管 <TT
CLASS="literal"
>!</TT
> 比 <TT
CLASS="literal"
>=</TT
> 的优先级高,PHP
     仍旧允许类似如下的表达式:<TT
CLASS="literal"
>if (!$a = foo())</TT
>,在此例中
     <TT
CLASS="literal"
>foo()</TT
> 的输出被赋给了 <CODE
CLASS="varname"
>$a</CODE
>。
    </P
></BLOCKQUOTE
></DIV
></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.expressions.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.operators.arithmetic.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
>