1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-23 22:52:08 +01:00
Files
archived-doc-zh/language/operators/logical.xml
2024-01-18 23:49:26 +08:00

107 lines
2.8 KiB
XML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: 43d07782b514d0c7a8487f2c74063739f302df8d Maintainer: mowangjuanzi Status: ready -->
<sect1 xml:id="language.operators.logical">
<title>逻辑运算符</title>
<titleabbrev>逻辑</titleabbrev>
<table>
<title>逻辑运算符</title>
<tgroup cols="3">
<thead>
<row>
<entry>例子</entry>
<entry>名称</entry>
<entry>结果</entry>
</row>
</thead>
<tbody>
<row>
<entry>$a and $b</entry>
<entry>And逻辑与</entry>
<entry>&true;,如果 <varname>$a</varname><varname>$b</varname> 都为 &true;</entry>
</row>
<row>
<entry>$a or $b</entry>
<entry>Or逻辑或</entry>
<entry>&true;,如果 <varname>$a</varname><varname>$b</varname> 任一为 &true;</entry>
</row>
<row>
<entry>$a xor $b</entry>
<entry>Xor逻辑异或</entry>
<entry>&true;,如果 <varname>$a</varname><varname>$b</varname> 任一为 &true;,但不同时是。</entry>
</row>
<row>
<entry>! $a</entry>
<entry>Not逻辑非</entry>
<entry>&true;,如果 <varname>$a</varname> 不为 &true;</entry>
</row>
<row>
<entry>$a &amp;&amp; $b</entry>
<entry>And逻辑与</entry>
<entry>&true;,如果 <varname>$a</varname><varname>$b</varname> 都为 &true;</entry>
</row>
<row>
<entry>$a || $b</entry>
<entry>Or逻辑或</entry>
<entry>&true;,如果 <varname>$a</varname><varname>$b</varname> 任一为 &true;</entry>
</row>
</tbody>
</tgroup>
</table>
<simpara>
“与”和“或”有两种不同形式运算符的原因是它们运算的优先级不同(见<link linkend="language.operators.precedence">运算符优先级</link>)。
</simpara>
<example>
<title>逻辑运算符示例</title>
<programlisting role="php">
<![CDATA[
<?php
// --------------------
// foo() 根本没机会被调用,被运算符“短路”了
$a = (false && foo());
$b = (true || foo());
$c = (false and foo());
$d = (true or foo());
// --------------------
// "||" 比 "or" 的优先级高
// 表达式 (false || true) 的结果被赋给 $e
// 等同于:($e = (false || true))
$e = false || true;
// 常量 false 被赋给 $ftrue 被忽略
// 等同于:(($f = false) or true)
$f = false or true;
var_dump($e, $f);
// --------------------
// "&&" 比 "and" 的优先级高
// 表达式 (true && false) 的结果被赋给 $g
// 等同于:($g = (true && false))
$g = true && false;
// 常量 true 被赋给 $hfalse 被忽略
// 等同于:(($h = true) and false)
$h = true and false;
var_dump($g, $h);
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
bool(true)
bool(false)
bool(false)
bool(true)
]]>
</screen>
</example>
</sect1>