Categories: TECHNOLOGY

Boolean Logic and PHP Conditions

Introduction
This is part 6 of my series, Basics of PHP. In this part of the series we apply Boolean logic to PHP conditions.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Single Expression Example
Consider the following code:

Read and try the code (you have to add the surrounding HTML elements first). In the condition, (parentheses of if) there is only one expression, which is, $me == “tall”. If this expression results in true, the if-block will be executed. The above if-statement is equivalent to

if (true)
{
echo ‘I am tall’;
}

For this second if-statement to be executed, you do not need the creation of the variable and its assignment. Read and try the following code:

Let us look at a case where the condition results in false. Consider the following code:

The if-block (curly braces) in the above code will not be executed, because the condition results in false, since the value of the variable, $me, is “short” and not “tall”. The above if-statement is equivalent to:

The if-block can only be executed if the condition is true. In this last case it is not executed.

More than One Expression in Condition
You can have more than one expression in a condition. In this part of the series, I consider a maximum of two expressions in a condition. Each of the expressions results in true or false. The expressions are combined with the AND, OR or NOT operators. The AND operator is typed as, &&. The OR operator is typed as, || . The NOT Operator is typed as ! . &&, || , and ! are called logical operators. With logical operators, the rules in the previous part of the series can be rewritten as:

AND
(false) && (false) = false
(false) && (true) = false
(true) && (false) = false
(true) && (true) = true

OR
(false) || (false) = false
(false) || (true) = true
(true) || (false) = true
(true) || (true) = true

NOT
!(false) = true
!(true) = false

Double-Expression Examples
The if-block will not be executed in the following code:

A practical example for the above code is:

“tall” is assigned to the variable, $you, and also to the variable, $me. The first expression in the condition results in false and the second one results in true. (false)&&(true) gives false as the effective Boolean value for the condition. So the block is not executed.

The if-block will be executed in the following code:

A practical example for the above code is:

Read the above code. Try it. The first expression results in false; the second one results in true. The effective condition is true, since (false)||(true) gives true.

NOT Examples
The if-block will be executed in the following code:

The if-block is executed, if the condition is true. !(false) gives true. If the condition is false, the if-block will not be executed.

A practical example for the above code is:

elseif and else
You can still add the elseif and else sub statements to the above code samples, following what we learned in one of the previous parts.

Let us stop here and continue in the next part of the series.

Chrys

Karla News

Recent Posts

Green River, Running Red by Ann Rule

Ann Rule is no doubt one of the world's unluckiest people when it comes to…

2 mins ago

Canada Lynx Facts

Lynx, lynx Canadensis, resemble gigantic housecats. They measure 31 to 41 inches (79 to 102…

7 mins ago

Review: Jiffy Pop Popcorn

About a month ago, I was watching an older movie (from the late 1980s) and…

13 mins ago

Cingular Wireless Cell Phone Plan Benefits

I have written this article to share with you about what I have learned about…

20 mins ago

Start a Worm Composting Bin with Your Kids

My crazy friend brought home about 300 new family pets the other day. Okay, they…

26 mins ago

Little Known Facts About Baseball’s Lawrence “Yogi” Berra

While many people recognize Yogi Berra from Alfac commercials, Berra has been using his "Yogi-isms"…

31 mins ago

This website uses cookies.