Monday, January 7, 2008

Not Equal To


$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!");
}

No comments: