PHP Comparison Operators

You saw in the last section how to test what is inside of a variable. You used if, else … if, and else. You used the double equals sign (==) to test whether the variable was the same thing as some direct text. The double equals sign is known as a Comparison Operator. There a few more of these “operands” to get used. Here’s a list. Take a look, and then we’ll see a few examples of how to use them.


Here's some more information on the above Operands.

== (Has the same value as)

The double equals sign can mean “Has a value of” or "Has the same value as”. In the example below, the variable called $variable1 is being compared to the variable called $variable2

if ($variable1 == $variable2) {
}
!= (Is NOT the same value as)

You can also test if one condition is NOT the same as another. In which case, you need the exclamation mark/equals sign combination ( != ). If you were testing for a genuine username, for example, you could say:

if ($what_user_entered != $username) {
print("You're not a valid user of this site!");
}

The above code says, “If what the user entered is NOT the same as the value in the variable called $username then print something out.

< (Less Than)

You'll want to test if one value is less than another. Use the left angle bracket for this ( < )
> (Greater Than)

You'll also want to test if one value is greater than another. Use the right angle bracket for this ( > )

<= (Less than or equals to)

For a little more precision, you can test to see if one variable is less than or equal to another. Use the left angle bracket followed by the equals sign ( <= )

>= (Greater than or equals to)

If you need to test if one variable is greater than or equal to another, use the right angle bracket followed by the equals sign ( >= )

In the next few sections, you'll see some examples of how to use the comparison operators. You've already used the double equals sign, so we'll start with "Not equal to".

In this lessons, we'll explore the Comparison Operator for Not Equal To: !=.
So open up your text editor, and add the following script:

<?PHP
$correct_username = 'logmein';
$what_visitor_typed = 'logMEin';
if ($what_visitor_typed != $correct_username) {
print("You're not a valid user of this site!");
}
?>

Save your work and try it out. You should be able to guess what it does! But the thing to note here is the new Comparison Operator. Instead of using the double equals sign we’re now using an exclamation mark and a single equals sign. The rest of the If Statement is exactly the same format as you used earlier.

The things you’re trying to compare need to be different before a value of true is returned by PHP. In the second variable ($what_visitor_typed), the letters “ME” are in uppercase; in the first variable, they are in lowercase. So the two are not the same. Because we used the NOT equal to operator, the text will get printed. Change your script to this:

$correct_username = 'logmein';
$what_visitor_typed = 'logmein';
if ($what_visitor_typed != $correct_username) {
print("You're not a valid user of this site!");
}
else {
print("Welcome back, friend!");
}

See if you can figure out what has changed. Before you run the script, what will get printed out?

In the next part, we'll have a look at how to use the Less Than ( < ) and Greater Than ( > ) operators.