Editor PHP Xiaoxin mendedahkan kepada anda kebenaran dan kepalsuan dalam PHP. Sebagai bahasa pengaturcaraan yang popular, PHP sering disalahfahamkan atau penyebaran maklumat yang salah. Artikel ini akan meneroka situasi sebenar dalam PHP dari pelbagai sudut untuk membantu pembaca lebih memahami kebaikan dan keburukan bahasa ini dan mengelak daripada ditipu oleh khabar angin. Marilah kita mendedahkan kebenaran tentang PHP bersama-sama!
Apabila menggunakan false
, ia tidak menunjukkan sebarang output. Perlu diingat bahawa pelayar sentiasa memaparkan nilai ini sebagai rentetan. false
时,它不显示任何输出。值得注意的是,浏览器总是以字符串的形式呈现这些值。
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><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>
var_dump()
函数确定布尔值我们还可以在 php.ini 中转换布尔值。让我们首先了解这些参数的一些预定义值分配。
如果我们将以下值转换为布尔值,它们将被视为假
true< dalam <strong class="keylink">php</strong> / code> dan <code>false
sebagai nilai logik Booleantrue
atau false
. false
是 false
<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>
var_dump()
dalam PHP untuk menentukan nilai boolean0
也是 false
Kami juga boleh menukar nilai boolean dalam php.ini. Mari kita fahami beberapa penetapan nilai pratakrif dahulu untuk parameter ini. false
: 0.0
和 -0.0
之类的浮点数也是 false
0
元素的数组是 false
. NULL
是 false
. . true
。
我们可以借助转储布尔值信息的 var_dump()
函数来确定布尔值。
让我们用 var_dump
.
🎜🎜. 🎜 🎜🎜Sebaliknya, semua nilai lain dianggapvar_dump()
yang membuang maklumat nilai Boolean. 🎜🎜Mari kami gunakan var_dump
untuk menyemak jenis ralat yang disebutkan di atas: 🎜rrreee 🎜Output: 🎜rrreeeAtas ialah kandungan terperinci Betul dan Salah dalam PHP. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!