0% found this document useful (0 votes)
21 views

PHP Tizag Tutorial-45

The for loop in PHP allows you to initialize a counter variable, check a conditional statement, execute code, and increment the counter in a single statement. It covers common looping tasks like setting an initial counter value, checking a condition, executing code, and incrementing the counter each iteration. The document provides an example of using a for loop to output a table showing the prices of brushes at different quantities, initializing the counter at 10, checking that it is less than or equal to 100, executing code to output the row, and incrementing the counter by 10 each time.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

PHP Tizag Tutorial-45

The for loop in PHP allows you to initialize a counter variable, check a conditional statement, execute code, and increment the counter in a single statement. It covers common looping tasks like setting an initial counter value, checking a condition, executing code, and incrementing the counter each iteration. The document provides an example of using a for loop to output a table showing the prices of brushes at different quantities, initializing the counter at 10, checking that it is less than or equal to 100, executing code to output the row, and incrementing the counter by 10 each time.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

PHP - For Loop

The for loop is simply a while loop with a bit more code added to it. The common tasks that are covered by
a for loop are:

1. Set a counter variable to some initial value.


2. Check to see if the conditional statement is true.
3. Execute the code within the loop.
4. Increment a counter at the end of each iteration through the loop.

The for loop allows you to define these steps in one easy line of code. It may seem to have a strange
form, so pay close attention to the syntax used!

For Loop Example

Let us take the example from the while loop lesson and see how it could be done in a for loop. The basic
structure of the for loop is as follows:

Pseudo PHP Code:


for ( initialize a counter; conditional statement; increment a counter){
do this code;
}

Notice how all the steps of the loop are taken care of in the for loop statement. Each step is separated by
a semicolon: initiliaze counter, conditional statement, and the counter increment. A semicolon is needed
because these are separate expressions. However, notice that a semicolon is not needed after the "increment
counter" expression.
Here is the example of the brush prices done with a for loop .

PHP Code:
$brush_price = 5;

echo "<table border=\"1\" align=\"center\">";


echo "<tr><th>Quantity</th>";
echo "<th>Price</th></tr>";
for ( $counter = 10; $counter <= 100; $counter += 10) {
echo "<tr><td>";
echo $counter;
echo "</td><td>";
echo $brush_price * $counter;
echo "</td></tr>";
}
echo "</table>";

You might also like