02.3c: Logical Operators

How do we make a control structure when we need to evaluate more than one condition?


https://www.youtube.com/watch?v=fj3KeH6qBrA





<?php
# * -----------------------------------------------------------
# * Logical operators
# * What if we want to test multiple conditionals at the same time, 
# * like $a > $b and $a < $c ?  We are now going to start looking at logical 
# * operators.
#    &&


$a = 5;
$b = 1;
$c = 10;

if ($a > $b && $a < $c)        {
    echo "a is greater than b and less than c\n";
}


# * Exercise:  Modify our Fahrenheit to Celsius conversion to print a message 
# * if the converted temp is greater than 16c and less than 26c: "The 
# * temperature is nice today.\n"




# * -----------------------------------------------------------
# * What if we want to test two conditionals with either that might be true 
# * instead of where both must be true?
#    ||

# * Note: If we have multiple || operators, the first time any condition is 
# * true, the test will return true.

$a = true;
$b = false;

if ($a || $b )        {
    echo "either a or b is true.\n";
}

# * Exercise: Write an if statement that, given the variables below, tests 
# * to see if $a is greater than $b or $a is less than $c:

$a = 5;
$b = 1;
$c = 3;




# * -----------------------------------------------------------
# * Short hand if statement.

$a = 5;
$b = 1;

$result = ($a > $b) ? "A is greater" : "B is greater";

echo "result: $result\n";




# * -----------------------------------------------------------
# * Nesting if statements

$a = true;
$b = false;

if ($a)                {
    echo "a is true.\n";

    if ($b)    {
        echo "b is true (this will not print).\n";
    }
}


# * Exercise: Modify the temperature conversion program to print a 
# * message "It might be cold", if the temp is below 18c.  Add a 
# * nested if to print a second message "Might want to bring a sweater" 
# * if the temp is below 14c.


?>