Maison >développement back-end >tutoriel php >Vrai et faux en PHP
L'éditeur PHP Xiaoxin vous révèle la vérité et le mensonge en PHP. En tant que langage de programmation populaire, PHP est souvent mal compris ou des informations erronées se propagent. Cet article explorera la situation réelle de PHP sous différents angles pour aider les lecteurs à mieux comprendre les avantages et les inconvénients de ce langage et à éviter de se laisser tromper par les rumeurs. Découvrons ensemble la vérité sur PHP !
Lors de l'utilisation de false
, il n'affiche aucune sortie. Il convient de noter que les navigateurs affichent toujours ces valeurs sous forme de chaîne. 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< dans <strong class="keylink">php</strong> / code> et <code>false
comme valeurs logiques booléennestrue
ou 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>Sortie :
<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()
en PHP pour déterminer la valeur booléenne0
也是 false
Nous pouvons également convertir des valeurs booléennes dans php.ini. Comprenons d'abord quelques attributions de valeurs prédéfinies pour ces paramètres. false
: 0.0
和 -0.0
之类的浮点数也是 false
0
元素的数组是 false
. NULL
是 false
. . true
。
我们可以借助转储布尔值信息的 var_dump()
函数来确定布尔值。
让我们用 var_dump
.
🎜🎜. 🎜 🎜🎜Au lieu de cela, toutes les autres valeurs sont considérées commetrue
. 🎜🎜Nous pouvons déterminer la valeur booléenne à l'aide de la fonction var_dump()
qui vide les informations sur la valeur booléenne. 🎜🎜Vérifions le type d'erreur mentionné ci-dessus en utilisant var_dump
: 🎜rrreee🎜Sortie : 🎜rrreeeCe qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!