Boolean Expressions

Sometimes a programmer would like one statement, or group of statements to execute only if certain conditions are true. There may be a different statement, or group of statements that are to be executed when the condition is false. An expression that can be evaluated to true or false is called a Boolean expression. (Named after a mathematician named Boole.)

In PHP, the number 0 (zero) is considered to be false, all other numbers are true. The null string, "", is false, all other strings are true. Also, if the variable has not been assigned a value, it evaluates to false.

Example:
Put this code on a test page, then give x different values in the URL: test.php?x=5 or test.php?x=0 or test.php?x=abc or just test.php
<?php
if($x) echo 'true';
else echo 'false';
?>

The Boolean operators

The Boolean operators are a bit different than the ones used in algebra because we have to be able to type them. (However, they are exactly the same as the ones used in C, C++, Java, and ActionScript!)
== equal (Remember, a single = is used to assign a value!)
< less than
> greater than
>= greater than or equal to
<= less than or equal to
!= not equal

Each expression below is TRUE given the values $x = 0; $y=5; $z=8;
$x < $y //0 is less than 5
$z > $y //8 is greater than 5
$y < $z //5 is less than 8
$y <= $z //5 is less than or equal to 8
$x !=$z //0 is not equal to 8
$z >= $y //8 is greater than or equal to 5


INDEX, Boolean Values, if/else, Summary, Compound Conditions: AND && and OR ||, SWitch
Next lesson: Important InformationMAIN INDEX
EXAMPLES INDEX

Copyright © Zebra0.com
All rights reserved worldwide.

 
 

Boolean Values