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>examnple</title>
</head>
<body>

<h1>I like eggs</h1>
<p>I like eggs so i eat eggs</p>

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

<?php
$eggs = 15;

Do
{
echo "<tr><td>";
echo "Hey I have tons of eggs! ";
echo " I have ".$eggs." eggs!";

echo "</tr></td>";

$eggs=$eggs-1;

if ($eggs==12)
{
break;
}

}
while ($eggs > 10) ;
if ($eggs == 10)
{
echo "<tr><td>:";
echo "Holly banna time for scrambled eggs!";
echo "</td></tr>";
}
else
{
echo "<tr><td>:";
echo "Hooly banna time for scrambled edds!";
echo "</td></tr>";
}
?>

</table>

</body>
</html>