Author: Sun Sports
Now, you understand that PHP allows you to nest conditional statements. However, if you look at the example used to demonstrate this concept you will agree that it is both complex and terrifying.
--------------------------------------------------------------------------------
< ?
if ($day == "Thursday")
{
if ($time == "12")
{
if ($place == "Italy")
{
$lunch = "pasta";
}
}
}
?>
--------------------------------------------------------------------------------
Fortunately, in addition to the comparison operators that we can already use without any restrictions, PHP also provides some logical operators to allow you to gather conditional statement descriptions. The following table clearly shows these:
Assume $delta = 12 and $omega = 9
Operator
significance
example
result
&&
AND
$delta == $gamma && $delta > $omega
True
$delta && $omega < $omega
False
||
OR
$delta == $gamma || $delta < $omega
True
$delta > $gamma || $delta < $omega
False
!
NOT
!$delta
False
< =
Less than or equal to
$delta < = $omega
False
OK, we can use logical operators to rewrite the code of the above example. You see, is the following statement simpler?
--------------------------------------------------------------------------------
< ?
if ($day == "Thursday" && $time == "12" && $place == "Italy")
{
$lunch = "pasta";
}
--------------------------------------------------------------------------------
Simple and elegant? Yes
Now, you understand that PHP allows you to nest conditional statements. However, if you look at the example used to demonstrate this concept you will agree that it is both complex and terrifying.
--------------------------------------------------------------------------------
< ?
if ($day == "Thursday")
{
if ($time == "12")
{
if ($place == "Italy")
{
$lunch = "pasta";
}
}
}
?>
--------------------------------------------------------------------------------
Fortunately, in addition to the comparison operators that we can already use without any restrictions, PHP also provides some logical operators to allow you to gather conditional statement descriptions. The following table clearly shows these:
Assume $delta = 12 and $omega = 9
Operator
significance
example
result
&&
AND
$delta == $gamma && $delta > $omega
True
$delta && $omega < $omega
False
||
OR
$delta == $gamma || $delta < $omega
True
$delta > $gamma || $delta < $omega
False
!
NOT
!$delta
False
< =
Less than or equal to
$delta < = $omega
False
OK, we can use logical operators to rewrite the code of the above example. You see, is the following statement simpler?
--------------------------------------------------------------------------------
< ?
if ($day == "Thursday" && $time == "12" && $place == "Italy")
{
$lunch = "pasta";
}
--------------------------------------------------------------------------------
Simple and elegant? Yes