Home > Backend Development > PHP Tutorial > True and False in PHP

True and False in PHP

王林
Release: 2024-02-28 10:32:01
forward
991 people have browsed it

php editor Xiaoxin reveals to you the truth and falsehood in PHP. As a popular programming language, PHP is often misunderstood or misinformation spread. This article will explore the real situation in PHP from various angles to help readers better understand the advantages and disadvantages of this language and avoid being misled by rumors. Let us uncover the truth about PHP together!

When using false, it does not show any output. It's worth noting that browsers always render these values ​​as String.


Using true and false as Boolean logical values ​​in php Boolean values ​​are logical values. They can be

true

or false. Both are case-sensitive in php and are also simple data types in 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>
Copy after login

Output:

<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>
Copy after login

Use the
var_dump()

function in PHP to determine the Boolean valueWe can also determine the Boolean value in php.ini Convert Boolean value. Let's first understand some predefined value assignments for these parameters.

If we convert the following values ​​to boolean, they will be treated as

false

:

    false
  • is false.
  • 0
  • is also false. Floating point numbers such as
  • 0.0
  • and -0.0 are also false. An
  • array
  • with 0 elements is false.
  • NULL
  • is false.
  • In contrast, all other values ​​are considered
true

. We can determine the Boolean value with the help of the

var_dump()

function that dumps Boolean value information. Let us check the above mentioned error type using

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>
Copy after login
Output:
<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>
Copy after login

The above is the detailed content of True and False in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:lsjlt.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template