Logical Operators in PHP
PHP Operators – Part 4
Forward: In this part of the series, we look at logical operators in PHP.
By: Chrysanthus Date Published: 10 Aug 2012
Introduction
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 Condition Expression Example
Consider the following code:
<?php
//tall means 20
$me = 20;
if ($me == 20)
{
echo "I am tall";
}
?>
Read and try the code. In the condition, (parentheses of if) there is only one expression, which is, ($me == 20). If this expression results in true, the if-block will be executed. In PHP the number 1 means true in a condition and the number 0 (zero) means false. In other words, 1 is a Boolean value for true and 0 is a Boolean value for false. The above if-construct is equivalent to
if (1)
{
echo "I am tall";
}
Here 1 is true. For this second if-construct to be executed, you do not need the creation of the variable and its assignment. Read and try the following code:
<?php
if (1)
{
echo "I am tall";
}
?>
<?php
//short means 10
$me = 10;
if ($me == 20)
{
echo "I am tall";
}
?>
The if-block (curly braces) in the above code will not be executed, because the condition results in false, since the value of the identifier, $me, is 10 for “short” and not 20 for “tall”. The above if-construct is equivalent to:
if (0)
{
echo "I am tall";
}
An if-block can only be executed if the condition is true. In this last case it is not executed, since zero means false.
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 || , (and sometimes !) are called logical operators. There are variations to this in PHP(see below). With logical operators, the rules (truth tables) for AND, OR and NOT can be written as:
(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:
<?php
if ((0)&&(1))
{
echo "We are tall";
}
?>
A practical example for the above code is:
<?php
//Tall means 20 and short means 10
$you = 20;
$me = 20;
if (($you == 10)&&($me == 20))
{
echo "We are tall";
}
?>
20 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:
<?php
if ((0)||(1))
{
echo "We are tall";
}
?>
A practical example for the above code is:
<?php
//Tall means 20 and short means 10
$you = 20;
$me = 20;
if (($you == 10)||($me == 20))
{
echo "We are tall";
}
?>
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.
The if-block will be executed in the following code:
<?php
if (!(0))
{
echo "I am tall";
}
?>
The if-block is executed, if the condition is true. !(false) gives true.
A practical example for the above code is:
<?php
//Let tall means 20 and short means 10
$me = 20;
if (!($me == 10))
{
echo "I am tall";
}
?>
The xor Operator
This is the Exclusive Or operator. The truth table for exclusive OR is:
XOR
(false) || (false) = false
(false) || (true) = true
(true) || (false) = true
(true) || (true) = false
Here, both of the operands cannot be true. Either the left or the right is true, not both. The difference between this truth table and the OR truth table is in the last line.
The and Operator
The and operator (in words) is the same as the && operator but has a different precedence (see later).
The or Operator
The or operator (in words) is the same as the || operator but has a different precedence (see later).
In PHP, you can use the word, true in place of 1 and false in place of 0 as in the following example.
<?php
if (true)
{
echo "Yes, you can replace 1 with true."."<br />";
}
if (!(false))
{
echo "Also, you can replace 0 with false.";
}
?>
In PHP logical operators are &&, ||, !, xor, and and or. All the logical operators are left-to-right operators except !, which is a right-to-left operator.
Time to take a break. We continue in the next part of the series.
Chrys
Related Links
Major in Website DesignWeb Development Course
HTML Course
CSS Course
ECMAScript Course
NEXT