PHP Not Equal To

 

In the previous section, you saw what Comparison Operators were. 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.