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:
| Do While Loops |
A Do While loops is similar to a While loop, however, a Do While loop always goes through the process code at least once. This is because the condition is tested after the process code. The process code will loop if the end condition is true.
Here's some code:
$dollars = 150;
Do
{
echo "Hey, doll, I'm loaded with cash!";
echo "I have ".$dollars." dollars. ";
$dollars=$dollars-1;
}
while ($dollars > 0) ;
The above code evaluates the condition at the end. If the condition evaluates as true, the process code will loop.
|
Here's another example:dowhileloops.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>
<title>dowhileloops.php</title>
<style>
table
{
margin:auto;
}
td
{
border: 1px solid black;
}
</style>
</head>
<body>
<h1>Mr. C. - Big Spender</h1>
<p>Mr. C. likes to take his ladies out and money is no object.
He goes all out and spends at leat $15.00 per date, but once
he only has $10.00 dollars, he stops spending and buys himself
a taco.</p>
<table>
<?php
//set dollar amount
$dollars = 15;
//run process code at least once and then test condition
Do
{
echo "
"; //create row and column
echo "Hey, doll, I'm loaded with cash! ";
echo " I have ".$dollars." dollars!";
echo " |
"; //close row and column
//lose a dollar
$dollars=$dollars-1;
}
while ($dollars > 10) ;//if you have more than 10 dollars loop
if ($dollars == 10)
{
echo ":";
echo "Oh, oh...time for a taco!";
echo " |
";
}
?>
</table>
</body>
</html>