PHP Index

In order to become familiar with PHP programming structures and commands, we will build various PHP pages.S 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:



If Statements

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

<html>
<head>
<title>ifstatements.php</title>
</head>
<body>

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:

Justin 89.4
Alicia 89
Manuel 90
Raul 91
Toni 89.3
Griselda 92
Granias 99
Jose 99.9

Let's use PHP to display the great news to each
student based on their exam result.

<?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&g;t");
}

?>

</body>
</html>