|
As previously mentioned, if statements need to evaluate to true or false.
Therefore, you need comparison operators to compare values in if statements.
Here is a list of comparison operators and several examples.
| Operator | Operation | Example | Result |
| == |
Equal |
5==6 |
False |
| === |
Identical |
5===5 |
True (if same datatype) |
| != |
Not Equal |
5!=4 |
True |
| <> |
Not Equal |
5<>6 |
True |
| !== |
Not Identical |
5!==6 |
True |
| < |
Less Than |
3<5 |
True |
| > |
Greater Than |
5>7 |
False |
| <= |
Less Than or Equal to |
9<=15 |
False |
| >= |
Greater Than or Equal to |
15>=5 |
True |
Here's some code:
//set the variable to 7
$dollars=7;
if ($dollars!=5)
{
print("You don't have 5 dollars!");
}
if ($dollars==7)
{
print("You have 7 dollars!");
}
The above code if statements evaluate to true and they print to the browser.
Here's another example:comparisons.php
|