Assignment037
If statements allow PHP to make decisions. Just give PHP a condition and it will evaluate the condition as true or false. If the condition is true, the next block of code will be executed.
Here's some code:
//set the variable to 7
$dollars=7;
if ($dollars>5)
{
print("You have more than 5 dollars!");
}
The above code evaluates to true, therefore "You have more than 5 dollars!" will display on the browser.
Here's another example:ifstatements.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>ifstatements.php</title>
</head>
<body>
<p>My Web 2 students have just taken their final exam.
It was very difficult so I decided to buy my 'A' students a new car.
Here are their scores: </p>
Justin 89.4<br>
Alicia 89<br>
Manuel 90<br>
Raul 91<br>
Toni 89.3<br>
Griselda 92<br>
Granias 99<br>
Jose 99.9<br>
<p>Let's use PHP to display the great news to each
student based on their exam result. </p>
<?php
//These are variables to hold the grades
$justinsgrade=89.4;
$aliciasgrade= 89;
$manuelsgrade= 90;
$raulsgrade=91;
$tonisgrade=89.3;
$griseldasgrade=92;
$grainasgrade=99;
$josesgrade=99.9;
//Here are the if statements to test
//whether students scored greater than 89.5 (I round up).
if ($justinsgrade >= 90)
{
print("Justin, you get a new car! A van.<br>");
}
if ($aliciasgrade >= 90)
{
print("Alicia, you get a new car! A bug.<br>");
}
if ($manuelsgrade >=90)
{
print("Manuel, you get a new car! A truck.<br>");
}
if ($raulssgrade >= 90)
{
print("Raul, you get a new car! A sports car.<br>");
}
if ($tonisgrade >= 90)
{
print("Toni, you get a new car! A station wagon. <br>");
}
if ($griseldasgrade >= 90)
{
print("Griselda, you get a new car! An RV. <br>");
}
if ($grainasgrade >= 90)
{
print("Grainas, you get a new car! A mini-van. <br>");
}
if ($josesgrade >= 90)
{
print("Jose, you get a new car! A sports car. <br>");
}
?>
</body>
</html>
</body>
</html>