php小编小新为您揭示php中的真假。php作为一种流行的编程语言,常常被人误解或传播虚假信息。本文将从各种角度探讨php中的真实情况,帮助读者更好地了解这门语言的优缺点,避免被谣言误导。让我们一起来揭开php的真相!
使用 false
时,它不显示任何输出。值得注意的是,浏览器总是以字符串的形式呈现这些值。
在 php 中使用 true
和 false
作为布尔逻辑值
布尔值是逻辑值。它们可以是真
或假
。
两者在 php 中都是区分大小写的,也是 php 中的简单数据类型。
<code><code class="language-php hljs" data-lang="php"><span style="display:flex;"><span> <span style="color:#19177c">$true</span> <span style="color:#666">=</span> <span style="color:#008000;font-weight:bold">True</span>; <span style="color:#408080;font-style:italic">//returns 1 </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span> <span style="color:#19177c">$false</span> <span style="color:#666">=</span> <span style="color:#008000;font-weight:bold">False</span>; <span style="color:#408080;font-style:italic">// return nothing </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#19177c">$true</span><span style="color:#666">.</span>PHP_EOL; </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#19177c">$false</span>; </span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">//using comparis<strong class="keylink">io</strong>n operator to check boolean values </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span><span style="color:#008000;font-weight:bold">if</span> (<span style="color:#19177c">$a</span> <span style="color:#666">==</span> <span style="color:#ba2121">"abc"</span>) { </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"return boolean value"</span>; <span style="color:#408080;font-style:italic">//returns nothing since $a is not equal to abc thus it is a false </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span> } </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">if</span> (<span style="color:#008000;font-weight:bold">TRUE</span>) { </span></span><span style="display:flex;"><span> <span style="color:#408080;font-style:italic">//because true means true </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"Your condition is executed!"</span><span style="color:#666">.</span>PHP_EOL; </span></span><span style="display:flex;"><span>} </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">if</span> (<span style="color:#008000;font-weight:bold">false</span>) { </span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"It will print nothing"</span>; <span style="color:#408080;font-style:italic">//false returns nothing </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span> } </span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">//Determine integer comparisions with boolean values </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#666">5</span><span style="color:#666"><span style="color:#666">10</span>; <span style="color:#408080;font-style:italic">//returns 1 since 5 is actually less ten (condition true) </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#666">10</span> <span style="color:#666">!=</span> <span style="color:#666">5</span>; <span style="color:#408080;font-style:italic">//returns 1 (true) </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#666">5</span> <span style="color:#666">==</span> <span style="color:#666">4</span>; <span style="color:#408080;font-style:italic">//false </span></span></span></span></code></code>
登录后复制
输出:
<code><code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>1 </span></span><span style="display:flex;"><span>Your condition is executed! </span></span><span style="display:flex;"><span>11 </span></span></code></code>
登录后复制
在 PHP 中使用 var_dump()
函数确定布尔值
我们还可以在 php.ini 中转换布尔值。让我们首先了解这些参数的一些预定义值分配。
如果我们将以下值转换为布尔值,它们将被视为假
:
-
false
是false
。 -
0
也是false
。 - 诸如
0.0
和-0.0
之类的浮点数也是false
。 - 具有
0
元素的数组是false
。 -
NULL
是false
。
相反,所有其他值都被认为是 true
。
我们可以借助转储布尔值信息的 var_dump()
函数来确定布尔值。
让我们用 var_dump
检查上面提到的错误类型:
<code><code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#666"></span>php </span></span><span style="display:flex;"><span>var_dump((bool) <span style="color:#008000;font-weight:bold">false</span>);<span style="color:#408080;font-style:italic">//(false) </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span>var_dump((bool) <span style="color:#ba2121">"0"</span>); <span style="color:#408080;font-style:italic">//(false) </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span>var_dump((bool) <span style="color:#666">0.0</span>);<span style="color:#408080;font-style:italic">//(false) </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span>var_dump((bool) <span style="color:#666">-</span><span style="color:#666">0.0</span>);<span style="color:#408080;font-style:italic">//(false) </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span>var_dump((bool) <span style="color:#008000;font-weight:bold">NULL</span>); <span style="color:#408080;font-style:italic">//(false) </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span>var_dump((bool) <span style="color:#008000;font-weight:bold">array</span>()); <span style="color:#408080;font-style:italic">//(false) </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">//true bools (a few examples) </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span>var_dump((bool)<span style="color:#008000;font-weight:bold">true</span>);<span style="color:#408080;font-style:italic">//true </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span>var_dump((bool)<span style="color:#666">1</span>);<span style="color:#408080;font-style:italic">//true </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span>var_dump((bool)<span style="color:#666">100</span>); <span style="color:#408080;font-style:italic">//true </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span>var_dump((bool) <span style="color:#008000;font-weight:bold">array</span>(<span style="color:#666">65</span>)); <span style="color:#408080;font-style:italic">//true </span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span><span style="color:#bc7a00">?></span><span > </span></span></span></code></code>
登录后复制
输出:
<code><code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>bool(false) </span></span><span style="display:flex;"><span>bool(false) </span></span><span style="display:flex;"><span>bool(true) </span></span><span style="display:flex;"><span>bool(false) </span></span><span style="display:flex;"><span>bool(false) </span></span><span style="display:flex;"><span>bool(false) </span></span><span style="display:flex;"><span>bool(false) </span></span><span style="display:flex;"><span>bool(true) </span></span><span style="display:flex;"><span>bool(true) </span></span><span style="display:flex;"><span>bool(true) </span></span><span style="display:flex;"><span>bool(true) </span></span></code></code>
登录后复制
以上就是PHP 中的真假的详细内容,更多请关注php中文网其它相关文章!