PHP - Loops
We use loops to repeat execution of block of code.
There are several loop types. Depending on situation, sometimes it is better to use one kind of loop than another, but every loop can be transformed into another one. This is not so important for beginners, you just need to know that there is good and better use of particular loop.
We have following loops:
for (*1; *2; *3)
{
// block
}
for loop is usually used when we know exactly how many times we need to execute repeating block of code.
for loop has three parameters. First, *1 is statement, and it is executed at the loop very start, and it often used for counter initialization. Second, *2 is a condition which is true or false. Condition is checked at every iteration start. If condition is true, execution will continue, if it is false execution will break. Third, *3 is statement and it is executed at the end of every loop iteration. It is usually used for counter increasing.
while (*)
{
// repeating block of code
}
while loop is usually used when we do not know exactly how many times we need to execute repeating block of code.
* is a condition which is true or false. For example, value > 5.
Condition is checked at every iteration start. If condition is true, execution will continue, if it is false execution will break.
do {
// repeating block of code
} while (*);
do-while loop is usually used when we do not know exactly how many times we need to execute repeating block of code, but we know that we need to execute it at least once.
do-while loop is same as while loop with difference that the condition is checked at every iteration end. Consequence is that repeating block of code in do-while loop is always executed at least once.
foreach (array as variable)
{
// repeating block of code
}
foreach loop is a special kind of for loop, timesaver, specifically designed for iterating through arrays. Repeating block of code will execute as many times as there are elements in array, and at every iteration, variable will get next value from an array.
foreach loop can only be used for iterating through arrays.
There are two important statements related to loops - break and continue. These two statements can be used only in loops, and they are used to control loop flow.
We use break to stop loop execution prematurely. For example, we need to stop loop execution if we are computing something and the error occurs.
We use continue when we want to start next iteration prematurely. For example, we need to continue to next iteration if current iteration does not meet certain condition, and there is no point to finish execution of repeating block of code, but there are other iterations which can meet that condition.
<head>
<title>Loops</title>
</head>
<body>
<h2>FOR loop</h2>
<p>We will print "Hello, World!!!" 10 times</p>
<?php
for ($i = 0; $i < 10; $i = $i + 1)
{
echo "Hello, World!!!<br>";
}
?>
<h2>WHILE loop</h2>
<p>We will print "Hello, World!!!" for 0.01 seconds</p>
<?php
$counter = 0;
$start_time = microtime();
while ($start_time > (microtime() - 0.01))
{
echo "Hello, World!!! ";
$counter = $counter + 1;
}
echo "<br>Loop executed " . $counter . " times.";
?>
<h2>DO-WHILE loop</h2>
<p>We will print "Hello, World!!!" for 0.01 seconds</p>
<?php
$counter = 0;
$start_time = microtime();
do
{
echo "Hello, World!!! ";
$counter = $counter + 1;
} while ($start_time > (microtime() - 0.01));
echo "<br>Loop executed " . $counter . " times.";
?>
<h2>FOREACH loop</h2>
<p>We will print values from array</p>
<?php
$address_book = array (
"Jessica" => "(415) 555-2946",
"Michelle" => "(925) 555-1274",
"Linda" => "(707) 555-3349" );
foreach($address_book as $name=>$phone)
echo $name . " - " . $phone . "<br>";
?>
<h2>BREAK and CONTINUE</h2>
<p>We will print as many even number, which can be divided by 4, in 0.01 second</p>
<?php
$start_time = microtime();
for ($i = 1; $i <= 1000000; $i = $i + 1)
{
if ($i % 2 == 1)
continue;
// Number is odd, there is no point to continue
// loop to check if it can be divided by 4
// so we are going to next loop iteration
if ($i % 4 == 0)
echo "Number " . $i . " is dividable by 4<br>";
if ($start_time < (microtime() - 0.01))
break;
// If we are out of time we will stop the loop
// Notice: at this point condition is still true
// but the loop quits
}
?>
</body>
</html>
Related Marakana Courses
- PHP and MySQL Bootcamp Training
September 23rd, 2007 at 5:29 pm
In dept tutorials. thank for sharing Marakana