02.3b: More Comparison Operators

As our programs grow and become more complex, we are going to want more decision making capability for our control structures and program flow.


https://www.youtube.com/watch?v=1fGLRcmMZDI





<?php
/* More comparison operators

# * ----------------------------------------------------------------------
PHP offers many more comparison operators for use inside our comparison tests.
We have already looked at some simple comparison operators...
>
<
==



# * ----------------------------------------------------------------------
In addition to testing if $a is greater then or less than $b, we may also want to 
test if $a is greater/less than or equal to $b.  How do we test for that?  PHP 
offers comparison operators for that:
<=
>=


$a = 5;
$b = 5;

if ($a <= $b )     {
    echo "a is less than or equal to b\n";
}



# * Exercise: Write a progam that takes numeric grade score (0 - 100) and prints a 
# * message "A" if $score is greater than or equal to 90.
# * Modify it to print a message "B" if $score is between 80 and 89.




# * ----------------------------------------------------------------------
Type juggling:
$a == $b    Is equal:  true if $a is equal to $b after type juggling
$a === $b    Is identical: true if $a is equal to $b, and they are of the same type


$qty = 20;

if ($qty == '20')     {
    echo "Result is 'equal'.\n";
}


$qty = 20;

if ($qty === '20')     {
    echo "Result is 'identical / equal of same type'.\n";
} else             {
    echo "Result is 'NOT identical / equal of same type'.\n";
}




# * ----------------------------------------------------------------------
We have looked at testing if something is equal.  What if we want to test when 
something is not equal?:
!=

$a = 5;
$b = 10;

if ($a != $b)        {
    echo "$a is not equal to $b\n";
}


# * Exercise: Write a program with two int variables: $a $b, and if they are not equal, 
# * print a message.




# * ----------------------------------------------------------------------
# * We can also use functions and test the return value from functions.
$var1 = "James";
$var2 = "james";

if (strcmp( $var1, $var2 ) == 0)    
    echo "Names match (case sensitive).\n";

if (strcmp( $var1, $var2 ) != 0)    
    echo "Names do not match (case sensitive).\n";




# * ----------------------------------------------------------------------
# * NOT '!' operator.
$a = true;

if (! $a)    
    echo "This code should NOT print.\n";
else
    echo "This code SHOULD print.\n";


# * Exercise: Rewrite the strcmp test with NOT '!' operator instead of testing 
# * for == 0.

*/
?>