Heim >Backend-Entwicklung >PHP-Tutorial >Richtig und falsch in PHP
PHP-Editor Xiaoxin enthüllt Ihnen die Wahrheit und Unwahrheit in PHP. Als beliebte Programmiersprache wird PHP oft missverstanden oder Fehlinformationen verbreitet. In diesem Artikel wird die reale Situation in PHP aus verschiedenen Blickwinkeln untersucht, um den Lesern zu helfen, die Vor- und Nachteile dieser Sprache besser zu verstehen und sich nicht durch Gerüchte in die Irre führen zu lassen. Lassen Sie uns gemeinsam die Wahrheit über PHP ans Licht bringen!
Bei Verwendung von false
wird keine Ausgabe angezeigt. Es ist erwähnenswert, dass Browser diese Werte immer als string darstellen. 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< in <strong class="keylink">php</strong> / code> und <code>false
als boolesche logische Wertetrue
oder false
sein. 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>Ausgabe:
<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()
in PHP, um den booleschen Wert zu bestimmen0
也是 false
Wir können auch boolesche Werte in php.ini konvertieren. Lassen Sie uns zunächst einige vordefinierte Wertzuweisungen für diese Parameter verstehen. false
behandelt: 0.0
和 -0.0
之类的浮点数也是 false
0
元素的数组是 false
. NULL
是 false
. . true
。
我们可以借助转储布尔值信息的 var_dump()
函数来确定布尔值。
让我们用 var_dump
.
🎜🎜. 🎜 🎜🎜Stattdessen werden alle anderen Werte alswahr
betrachtet. 🎜🎜Wir können den booleschen Wert mithilfe der Funktion var_dump()
ermitteln, die boolesche Wertinformationen ausgibt. 🎜🎜Lassen Sie uns den oben genannten Fehlertyp mit var_dump
überprüfen: 🎜rrreee🎜Ausgabe: 🎜rrreeeDas obige ist der detaillierte Inhalt vonRichtig und falsch in PHP. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!