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
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:
< (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"
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.
== (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"
0 comments:
Post a Comment