首页 > 后端开发 > php教程 > PHP 中的真假

PHP 中的真假

王林
发布: 2024-02-28 10:32:01
转载
1004 人浏览过

php小编小新为您揭示PHP中的真假。PHP作为一种流行的编程语言,常常被人误解或传播虚假信息。本文将从各种角度探讨PHP中的真实情况,帮助读者更好地了解这门语言的优缺点,避免被谣言误导。让我们一起来揭开PHP的真相!

使用 false 时,它不显示任何输出。值得注意的是,浏览器总是以字符串的形式呈现这些值。


php 中使用 truefalse 作为布尔逻辑值

布尔值是逻辑值。它们可以是

两者在 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><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></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 中转换布尔值。让我们首先了解这些参数的一些预定义值分配。

如果我们将以下值转换为布尔值,它们将被视为

  • falsefalse
  • 0 也是 false
  • 诸如 0.0-0.0 之类的浮点数也是 false
  • 具有 0 元素的数组false
  • NULLfalse

相反,所有其他值都被认为是 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中文网其他相关文章!

来源:lsjlt.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板