PHP for Loop statement usage and for usage

Source: Internet
Author: User

The number of times to execute the same code set is used in PHP. The basic syntax of the author is as follows:

The For loop is just a little bit more code, and loops are added to it. And one of the common tasks involved in the cycle is:

Sets the initial values of some counter variables.
Please check that the conditional statement is correct.
The code loop in execution.
The increment ends at the end of each iteration through the loop counter.
The For loop allows you to define a simple line of code for these steps. It seems to have a strange form, so be sure to pay close attention to the grammar!

for (expression 1, expression 2, expression 3)
{
[Code to execute]
}


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

Look at a simple example of a

for ($i = 0; $i <= 2; $i + +)
{
print ' value is now '. $i. "<br>";
}

Output value

Value is now 0
Value is now 1
Value is now 2

In the first cycle, $i = 0, which means expression, ($i <= 2), for ture. Therefore, when the print statement executes, the $i is added by 1 and becomes 1.

In the second cycle, $ = 1, which means an expression, ($i <= 2), for ture. Therefore, when the print statement executes, the $i is added by 1 and becomes 2.

In the third iteration, $i = 2, which means expression, ($i <= 2), for ture. Therefore, when the print statement executes, the $i increments to 1 3.

In the fourth iteration, $i = 3, which means the expression, ($i <= 2), is false. Therefore, PHP does not execute loops and does not execute print statements.

Example Two

$brush _price = 5;

echo "<table border=" 1 "align=" center ">";
echo "<tr><th>Quantity</th>";
echo "<th>Price</th></tr>";
for ($counter = $counter <= $counter + + 10) {
echo "<tr><td>";
Echo $counter;
echo "</td><td>";
echo $brush _price * $counter;
echo "</td></tr>";
}
echo "</table>";

Output value

Quantity Price
10 50
20 100
30 150
40 200
50 250
60 300
70 350
80 400
90 450
100 500

Article Original in Www.111cn.net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.