PHP Index
In order to become familiar with PHP programming structures and commands, we will build various PHP pages.Sections of this page will explain each PHP command, provide a link to a working sample, and detail the working sample's PHP code. Let's get started:
| Comparisons |
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
|
<html>
<head>
<title>comparisons.php</title>
</head>
<body>
<p>
My Web 2 students have worked hard all year. Therefore, I've
been thinking about throwing them a pizza party.
If they score well on my class quality rating program,
I will throw them a party before Spring Break.</p>
<p>They will be rated on:</p>
<ul>
<li>Behavior </li>
<li>Attention</li>
<li>Cooperation</li>
<li>Effort</li>
<li>Enthusiasmt</li>
<ul>
<p>Let's use PHP to rate them on each category. </p>
<?php
//These are variables to hold the ratings
$behavior = "good";
$attention = "bad";
$cooperation = 9.5;
$effort = "bad";
$enthusiasm = 8.5;
//Here are the if statements and comparison conditions to rate them
if ($behavior!="bad") //not equal to
{
print("Bahavior was good.<br>");
}
if ($attention == "good") //equal to
{
print("Attention was good.<br>");
}
if ($cooperation >=9) //greater than or equal to
{
print("Cooperation was good. <br>");
}
if ($effort==="bad") //identical to (including data type)
{
print("Effort was bad. <br>");
}
if ($enthusiasm <7) //less than
{
print("Enthusiasm was bad. <br>");
}
?>
<p>Hmmm...based on the results, it looks like we are destined for ...?</p>
</body>
</html>