linuxStat PHP Documentation

Assignment to boolean variables

A boolean variable is assigned a condition.
So it is important to understand how conditions are constructed.
I will explain with a few examples.

Example 5.1:
7 < 13
is a condition. This condition has the value TRUE.

Example 5.2:
13 > 82
is another condition. This condition has the value FALSE.

Example 5.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.
$currentPrice < 18.05
is a condition. This condition has the value TRUE if the value of
$currentPrice is less than 18.05. If the value of $currentPrice is
18.05 or greater then this condition has the value FALSE.

A condition has one of two values: TRUE, FALSE.

The above examples construct conditions by comparing two integers or floats.
There are other ways to construct conditions. I will explain below.

The following is an example of how a condition is assigned to a boolean variable.

Example 5.4:
$age = 21;
$preteen = $age < 13;

In this example, $age is an integer variable. Its value is 21.
$preteen is a boolean variable. Since 21 is not less than 13,
the value of the condition $age < 13 is FALSE.
Since the condition $age < 13 is assigned to the variable $preteen,
the value of $preteen is FALSE.

The boolean And operator

A condition can be constructed by using multiple conditions and
boolean operators. For example, we can apply the && boolean
operator to two conditions. This operator is called And. The new
condition will have the value TRUE if both the conditions are TRUE.
In every other case the new condition will be FALSE.

Example 5.5:
(13 > 7) && (69 < 97)
Since 13 > 7 is TRUE and 69 < 97 is TRUE,
the above condition is TRUE.

Example 5.6:
(91 >= 101) && (6 <= 9)
Since 91 >= 101 is FALSE and 6 <= 9 is TRUE,
the above condition is FALSE.
The notation >= stands for "greater than or equal".
The notation <= stands for "less than or equal".

Example 5.7:
(672 == 672) && (1004 != 1004)
Since 672 == 672 is TRUE and 1004 != 1004 is FALSE,
the above condition is FALSE.
The notation == is used to test for equality.
The notation != is used to test for inequality.

Example 5.8:
(13 >= 101) && (14 < 13)
Since 13 >= 101 is FALSE and 14 < 13 is FALSE,
the above condition is FALSE.

Example 5.9:
$age = 15;                            // Line 1
$notPreteen = $age > 12;              // Line 2
$notAdult = $age < 20;                // Line 3
$teenager = $notPreteen && $notAdult; // Line 4

$age is an integer variable.
$notPreteen, $notAdult and $teenager are boolean variables.
In Line 1 $age is assigned the value 15.
In Line 2, since 15 is greater than 12, $notPreteen is TRUE.
In Line 3, since 15 is smaller than 20, $notAdult is TRUE.
In Line 4, since $notPreteen and $notAdult are TRUE,
$teenager is TRUE.

The PHP program in Example 5.9 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
# Example of how boolean operator && works.
$age = 15;      // Line 1
print "After Line 1 the value of \$age is $age <br>";
$notPreteen = $age > 12;   // Line 2
print "After Line 2 \$notPreteen is $notPreteen <br>";
$notAdult = $age < 20;   // Line 3
print "After Line 3 \$notAdult is $notAdult <br>";
$teenager = $notPreteen && $notAdult   // Line 4
print "After Line 4 \$teenager is $teenager <br>";
?>
</font>
</body>
</html>

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

In the output, the integer value 1 is substituted for TRUE.

Try the above example    Next    Index