php小編小新為您揭示PHP中的真假。 PHP作為一種流行的程式語言,常常被誤解或傳播假訊息。本文將從各種角度探討PHP中的真實情況,幫助讀者更了解這門語言的優缺點,避免被謠言誤導。讓我們一起來揭開PHP的真相吧!
使用 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 中轉換布林值。讓我們先了解這些參數的一些預定義值分配。
如果我們將以下值轉換為布林值,它們將被視為假
:
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中文網其他相關文章!