linuxStat PHP Documentation

Construction of a condition using the boolean operator Not

Another method to construct a condition is to apply the !
boolean operator to a condition. This operator is called Not.
The new condition will have the value FALSE
if the condition has the value TRUE.
The new condition will have the value TRUE
if the condition has the value FALSE.

Example 6.1:
!(7 > 13)
Since 7 > 13 is FALSE the above condition is TRUE.

Example 6.2:
!(101 >= 91)
Since 101 >= 91 is TRUE the above condition is FALSE.

Example 6.3:

I have 1005 Intel (INTC) shares in my Fidelity Investments broker account.
I bought them at $18.05 a share. I keep track of my profit or loss in a PHP
program using a variable named $currentPrice. The value of this variable
is the current price of 1 share of Intel stock. I have a boolean variable named
$loss to determine if I would lose money if I sell my shares at the current price.
$currentPrice = 17.95;                            // Line 1
$loss = !($currentPrice >= 18.05);         // Line 2

In Line 1 $currentPrice is assigned the value 17.95.
In Line 2, since 17.95 is not greater than or equal to 18.05,
the condition $currentPrice >= 18.05 is FALSE.
So the condition !($currentPrice >= 18.05) is TRUE.
Hence $loss is TRUE.

The PHP program in Example 6.3 embedded in an HTML document:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/transitional.dtd">
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<title>Executed on Dell Server, Intel Processor, created on HP laptop</title>
</head>
<body bgcolor="#0F243E" text="#FFFF00" lang=EN-US>
<font size="5">
<?php
# An example of how boolean operator ! works.
$currentPrice = 17.95;      // Line 1
print "After Line 1 the value of \$currentPrice is $currentPrice <br>";
$loss = !($currentPrice >= 18.05);   // Line 2
print "After Line 2 \$loss is $loss <br>";
?>
</font>
</body>
</html>

Click the following link to execute the above PHP code
by my server's PHP interpreter.

Try the above example


Construction of a condition using the boolean operator Or

Another method to construct a condition is to apply the ||
boolean operator to two conditions. This operator is called Or.
The new condition will have the value TRUE if one or more
of the conditions are TRUE.

Example 6.4:
(7 > 13) || (97 < 69)
Since 7 > 13 is FALSE and 97 < 69 is FALSE,
the above condition is FALSE.

Example 6.5:
(101 >= 91) || (9 <= 6)
Since 101 >= 91 is TRUE the above condition is TRUE.

Example 6.6:
(672 != 672) && (1004 == 1004)
Since 1004 == 1004 is TRUE the above condition is TRUE.

Example 6.7:
(13 <= 101) || (713 < 2000)
Since 13 <= 101 is TRUE and 713 < 2000 is TRUE,
the above condition is TRUE.

Example 6.8:
$age = 15;                         // Line 1
$preteen = $age < 13;              // Line 2
$adult = $age >= 20;               // Line 3
$notTeenager = $preteen || $adult; // Line 4

$age is an integer variable.
$preteen, $adult and $notTeenager are boolean variables.
In Line 1 $age is assigned the value 15.
In Line 2, since 15 is not less than 13, $preteen is FALSE.
In Line 3, since 15 is not greater than or equal to 20, $adult is FALSE.
In Line 4, since $preteen and $adult are FALSE
$notTeenager is FALSE.

The PHP program in Example 6.8 embedded in an HTML document:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/transitional.dtd">
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<title>Executed on Dell Server, Intel Processor, created on HP laptop</title>
</head>
<body bgcolor="#0F243E" text="#FFFF00" lang=EN-US>
<font size="5">
<?php
# An example of how boolean operator || works.
$age = 15;      // Line 1
print "After Line 1 the value of \$age is $age <br>";
$preteen = $age < 13;   // Line 2
$notPreteen = !$preteen;   // Line 3
print "After Line 3 \$preteen is $preteen, \$notPreteen is $notPreteen <br>";
$adult = $age >= 20;   // Line 4
$notAdult = !$adult;   // Line 5
print "After Line 5 \$adult is $adult, \$notAdult is $notAdult <br>";
$notTeenager = $preteen || $adult;   // Line 6
$teenager = !$notTeenager; // Line 7
print "After Line 7 \$notTeenager is $notTeenager, \$teenager is $teenager <br>";
?>
</font>
</body>
</html>

Click the following link to execute the above PHP code
by my server's PHP interpreter.

Unfortunately, the PHP print statement prints nothing in the output
for the boolean value FALSE. You will see that in the output
for the variables expected to be FALSE.

Try the above example    Next    Index