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:



Else Statements
Else statements allow alternatives to PHP if statements. If a condition is false, the else statement will execute.

Here's some code:

//set the variable to 10
$dollars=10;

if ($dollars>20)
{
print("You have more than 20 dollars!");
}
else
{
print("You don't have more than 20 dollars!");
}

The above code evaluates to false, therefore the else statement executes. "You don't have more than 20 dollars!" will display on the browser.
Here's another example:elsestatements.php

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

<p>My Web 2 students have just taken their PHP test.
It was very difficult, so I decided to buy my 'A' students new computers.
All other students get a bag of candy and a pat on the back.
Here are their scores: </p>

Mayra 99.9<br>
Lucy 79<br>
Zack 89<br>
Ariel 89<br>
John-Luke 69.9<br>
Miggy 89<br>
Lorenzo 89<br>
Kourtney 70.9<br>

<p>Let's use PHP to display the great news or bad news to each student based on their test result. </p> <?php
//These are variables to hold the grades
$mayrasgrade=99.9;
$lucysgrade= 79;
$zacksgrade= 89;
$arielsgrade=89;
$johnlukesgrade=69.9;
$miggysgrade=89;
$lorenzosgrade=89;
$kourtneysgrade=70.9;

//Here are the if statements to test
//whether students scored greater than 89.5 (I round up).
if ($mayrasgrade >89.5)
{
print("Mayra, you get a new computer!<br>");
}
else
{
print("Mayra, better luck next time. Enjoy the candy, kid.<br>");
}

if ($lucysgrade >89.5)
{
print("Lucy, you get a new computer!<br>");
}
else
{
print("Lucy, better luck next time. Don't eat it at once.<br>");
}

if ($zacksgrade >89.5)
{
print("Zack, you get a new computer!<br>");
}
else
{
print("Zack, better luck next time. Don't tell anyone. The candy is old.
");
}

if ($arielsgrade >89.5)
{
print("Ariel, you get a new computer!<br>");
}
else
{
print("Ariel. better luck next time, and leave Joya alone.<br>");
}

if ($johnlukesgrade >89.5)
{
print("John-Luke, you get a new computer!<br>");
}
else
{
print("John-Luke, better luck next time. Sorry I'm out of candy.<br>");
}

if ($miggysgrade >89.5)
{
print("Miggy, you get a new computer!");
}
else
{
print("Miggy, better luck next time. Ask John-Luke for your candy.<br>");
}

if ($lorenzosgrade >89.5)
{
print("Lorenzo, you get a new computer!<br>");
}
else
{
print("Lorenzo, better luck next time. Here's the last of my new candy. Don't tell anyone.<br>");
}
//Check this out! You can add other conditions using elseif!

if ($kourtneyscosgrade >89.5)
{
print("Kourtney, you get a new computer!
");
}
elseif ($kourtneysgrade > 79.5)
{
print("Kourtney, you get a new computer book!");
}
elseif ($kourtneysgrade > 69.5)
{
print("Kourtney, you get a new pencil!");
}
else
{
print("Kourtney, better luck next time. <br>");
}

?>

</body>
</html>