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:

Break

Loops are very useful, but sometimes you will need to break out of a loop a little early. You can set a condition to cause a break in the loop. Implement the break with the break statement.

Here's some code:

$dollars = 100;
Do
{
echo "Hey, doll, I'm loaded with cash!";
echo "I have ".$dollars." dollars.<br>";

$dollars=$dollars-1;

if ($dollars==20)
{
break ; // I need to get a haircut
}
}
while ($dollars > 0) ;

The above code uses a break to terminate the Do While Loop early. In this case, if dollars is equal to $20.00 the loop is terminated.

Here's another example:break.php

<html>
<head>
<title>break.php</title>
</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>

<p>This time, however, he wants a burrito, so he needs two extra bucks.
He will stop bragging about his cash reserve a little earlier (at 12 dollars).</p>

<table border="1" align="center">

<?php
//set dollar amount
$dollars = 15;

//run process code at least once and then test condition
Do
{
echo "<tr><td>"; //create row and column

echo "Hey, doll, I'm loaded with cash! ";
echo " I have ".$dollars." dollars!";

echo "</tr></td>"; //close row and column

//lose a dollar
$dollars=$dollars-1;

if ($dollars==12)
{
break; //
}

}
while ($dollars > 10) ;//if you have more than 10 dollars loop

if ($dollars == 10)
{
echo "<tr><td>:";
echo "Oh, oh...time for a taco!";
echo "</td></tr>";
}
else
{
echo "<tr><td>:";
echo "Oh, oh...time for a burrito!";
echo "</td></tr>";
}
?>

</table>

</body>
</html>