⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 language.operators.comparison.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
📖 第 1 页 / 共 2 页
字号:
     <tbody valign="middle" class="tbody">      <tr valign="middle">       <td colspan="1" rowspan="1" align="left"><a href="language.types.null.html" class="type null">null</a> or <a href="language.types.string.html" class="type string">string</a></td>       <td colspan="1" rowspan="1" align="left"><a href="language.types.string.html" class="type string">string</a></td>       <td colspan="1" rowspan="1" align="left">Convert <b><tt>NULL</tt></b> to &quot;&quot;, numerical or lexical comparison</td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left"><a href="language.types.boolean.html" class="type bool">bool</a> or <a href="language.types.null.html" class="type null">null</a></td>       <td colspan="1" rowspan="1" align="left">anything</td>       <td colspan="1" rowspan="1" align="left">Convert to <a href="language.types.boolean.html" class="type bool">bool</a>, <b><tt>FALSE</tt></b> &lt; <b><tt>TRUE</tt></b></td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left"><a href="language.types.object.html" class="type object">object</a></td>       <td colspan="1" rowspan="1" align="left"><a href="language.types.object.html" class="type object">object</a></td>       <td colspan="1" rowspan="1" align="left">Built-in classes can define its own comparison, different classes        are uncomparable, same class - compare properties the same way as        arrays (PHP 4), PHP 5 has its own <a href="language.oop5.object-comparison.html" class="link">explanation</a>       </td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left"><a href="language.types.string.html" class="type string">string</a>, <a href="language.types.resource.html" class="type resource">resource</a> or <a href="language.pseudo-types.html#language.types.number" class="type number">number</a></td>       <td colspan="1" rowspan="1" align="left"><a href="language.types.string.html" class="type string">string</a>, <a href="language.types.resource.html" class="type resource">resource</a> or <a href="language.pseudo-types.html#language.types.number" class="type number">number</a></td>       <td colspan="1" rowspan="1" align="left">Translate strings and resources to numbers, usual math</td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left"><a href="language.types.array.html" class="type array">array</a></td>       <td colspan="1" rowspan="1" align="left"><a href="language.types.array.html" class="type array">array</a></td>       <td colspan="1" rowspan="1" align="left">Array with fewer members is smaller, if key from operand 1 is not        found in operand 2 then arrays are uncomparable, otherwise - compare        value by value (see following example)</td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left"><a href="language.types.array.html" class="type array">array</a></td>       <td colspan="1" rowspan="1" align="left">anything</td>       <td colspan="1" rowspan="1" align="left"><a href="language.types.array.html" class="type array">array</a> is always greater</td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left"><a href="language.types.object.html" class="type object">object</a></td>       <td colspan="1" rowspan="1" align="left">anything</td>       <td colspan="1" rowspan="1" align="left"><a href="language.types.object.html" class="type object">object</a> is always greater</td>      </tr>     </tbody>    </colgroup>   </table>   <p class="para">    <div class="example">     <p><b>Example #1 Transcription of standard array comparison</b></p>     <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Arrays&nbsp;are&nbsp;compared&nbsp;like&nbsp;this&nbsp;with&nbsp;standard&nbsp;comparison&nbsp;operators<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">standard_array_compare</span><span style="color: #007700">(</span><span style="color: #0000BB">$op1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$op2</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$op1</span><span style="color: #007700">)&nbsp;&lt;&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$op2</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$op1&nbsp;&lt;&nbsp;$op2<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}&nbsp;elseif&nbsp;(</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$op1</span><span style="color: #007700">)&nbsp;&gt;&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$op2</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$op1&nbsp;&gt;&nbsp;$op2<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$op1&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">array_key_exists</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$op2</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;uncomparable<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}&nbsp;elseif&nbsp;(</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">$op2</span><span style="color: #007700">[</span><span style="color: #0000BB">$key</span><span style="color: #007700">])&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;elseif&nbsp;(</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">$op2</span><span style="color: #007700">[</span><span style="color: #0000BB">$key</span><span style="color: #007700">])&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$op1&nbsp;==&nbsp;$op2<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>     </div>    </div>    </p>   <p class="para">    See also <a href="function.strcasecmp.html" class="function">strcasecmp()</a>,    <a href="function.strcmp.html" class="function">strcmp()</a>,    <a href="language.operators.array.html" class="link">Array operators</a>,    and the manual section on    <a href="language.types.html" class="link">Types</a>.   </p>   <div id="language.operators.comparison.ternary" class="sect2">    <h3 class="title">Ternary Operator</h3>    <p class="para">     Another conditional operator is the &quot;?:&quot; (or ternary) operator.     <div class="example">      <p><b>Example #2 Assigning a default value</b></p>      <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Example&nbsp;usage&nbsp;for:&nbsp;Ternary&nbsp;Operator<br /></span><span style="color: #0000BB">$action&nbsp;</span><span style="color: #007700">=&nbsp;(empty(</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'action'</span><span style="color: #007700">]))&nbsp;?&nbsp;</span><span style="color: #DD0000">'default'&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'action'</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">//&nbsp;The&nbsp;above&nbsp;is&nbsp;identical&nbsp;to&nbsp;this&nbsp;if/else&nbsp;statement<br /></span><span style="color: #007700">if&nbsp;(empty(</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'action'</span><span style="color: #007700">]))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$action&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'default'</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$action&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'action'</span><span style="color: #007700">];<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>      </div>     </div>      The expression <i>(expr1) ? (expr2) : (expr3)</i>     evaluates to <span class="replaceable">expr2</span> if     <span class="replaceable">expr1</span> evaluates to <b><tt>TRUE</tt></b>, and     <span class="replaceable">expr3</span> if     <span class="replaceable">expr1</span> evaluates to <b><tt>FALSE</tt></b>.    </p>    <blockquote><p><b class="note">Note</b>:      <span class="simpara">      Please note that the ternary operator is a statement, and that it      doesn&#039;t evaluate to a variable, but to the result of a statement. This      is important to know if you want to return a variable by reference.      The statement <i>return $var == 42 ? $a : $b;</i> in a      return-by-reference function will therefore not work and a warning is      issued in later PHP versions.     </span>    </p></blockquote>    <blockquote><p><b class="note">Note</b>:            Is is recommended that you avoid &quot;stacking&quot; ternary expressions. PHP&#039;s      behaviour when using more than one ternary operator within a single      statement is non-obvious:      <div class="example">       <p><b>Example #3 Non-obvious Ternary Behaviour</b></p>       <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;on&nbsp;first&nbsp;glance,&nbsp;the&nbsp;following&nbsp;appears&nbsp;to&nbsp;output&nbsp;'true'<br /></span><span style="color: #007700">echo&nbsp;(</span><span style="color: #0000BB">true</span><span style="color: #007700">?</span><span style="color: #DD0000">'true'</span><span style="color: #007700">:</span><span style="color: #0000BB">false</span><span style="color: #007700">?</span><span style="color: #DD0000">'t'</span><span style="color: #007700">:</span><span style="color: #DD0000">'f'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;however,&nbsp;the&nbsp;actual&nbsp;output&nbsp;of&nbsp;the&nbsp;above&nbsp;is&nbsp;'t'<br />//&nbsp;this&nbsp;is&nbsp;because&nbsp;ternary&nbsp;expressions&nbsp;are&nbsp;evaluated&nbsp;from&nbsp;left&nbsp;to&nbsp;right<br /><br />//&nbsp;the&nbsp;following&nbsp;is&nbsp;a&nbsp;more&nbsp;obvious&nbsp;version&nbsp;of&nbsp;the&nbsp;same&nbsp;code&nbsp;as&nbsp;above<br /></span><span style="color: #007700">echo&nbsp;((</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #DD0000">'true'&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #DD0000">'false'</span><span style="color: #007700">)&nbsp;?&nbsp;</span><span style="color: #DD0000">'t'&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #DD0000">'f'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;here,&nbsp;you&nbsp;can&nbsp;see&nbsp;that&nbsp;the&nbsp;first&nbsp;expression&nbsp;is&nbsp;evaluated&nbsp;to&nbsp;'true',&nbsp;which<br />//&nbsp;in&nbsp;turn&nbsp;evaluates&nbsp;to&nbsp;(bool)true,&nbsp;thus&nbsp;returning&nbsp;the&nbsp;true&nbsp;branch&nbsp;of&nbsp;the<br />//&nbsp;second&nbsp;ternary&nbsp;expression.<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>       </div>      </div>     <br />    </p></blockquote>   </div>  </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.operators.bitwise.html">Bitwise Operators</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.operators.errorcontrol.html">Error Control Operators</a></div> <div class="up"><a href="language.operators.html">Operators</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -