The PHP for Loop statement uses the method to explain the _php Foundation in detail

Source: Internet
Author: User
Tags rand
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!

The syntax for the For loop is:

for (EXPR1; expr2; expr3)
Statement

The first expression (EXPR1) is evaluated unconditionally before the start of the loop.

EXPR2 is evaluated before each loop starts. If the value is TRUE, the loop continues and the nested loop statement executes. If the value is FALSE, the loop is terminated.

The EXPR3 is evaluated after each loop (execution).

Each expression can be empty or contain multiple expressions that are comma-delimited. In an expression expr2, all comma-delimited expressions are evaluated, but only the last result is taken. Expr2 is empty means an infinite loop (like C, PHP thinks its value is TRUE). This may not be as useless as you might think, because you will often want to end the loop with a break statement instead of using a for expression truth value.


Look at a simple example of a
Copy Code code as follows:

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
Copy Code code as follows:

$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

You can refer to the following examples, all of which show numbers 1 through 10:
Copy Code code as follows:

<?php
/* Example 1 * *

for ($i = 1; $i <= $i + +) {
echo $i;
}

/* Example 2 * *

for ($i = 1;; $i + +) {
if ($i > 10) {
Break
}
echo $i;
}

/* Example 3 * *

$i = 1;
for (;;) {
if ($i > 10) {
Break
}
echo $i;
$i + +;
}

/* Example 4 * *

for ($i = 1, $j = 0; $i <= $j + = $i, print $i, $i + +);
?>

Of course, the first example looks the most normal (or fourth), but users may find it convenient to use empty expressions in a for loop on many occasions.

PHP also supports the substitution syntax for loops with colons.
Copy Code code as follows:

for (EXPR1; expr2; expr3):
Statement
...
ENDfor;

We often need to iterate over arrays such as the following:

Copy Code code as follows:

<?php
/*
* We want to change the values of some elements in the following array during the traversal process
*/
$people = Array (
Array (' name ' => ' Kalle ', ' salt ' => 856412),
Array (' name ' => ' Pierre ', ' salt ' => 215863)
);

for ($i = 0; $i < sizeof ($people); + + $i)
{
$people [$i] [' salt '] = rand (000000, 999999);
}
?>

The problem with the above code is that the second expression for will cause the code to execute very slowly--because the length of the array is computed over each loop. Since the length of the array is constant, we can use an intermediate variable to store the length of the array, and then use that variable as the second expression for the loop. This allows the value of the variable to be used directly at the time of the loop, without having to recalculate each time. As follows:

Copy Code code as follows:

<?php
$people = Array (
Array (' name ' => ' Kalle ', ' salt ' => 856412),
Array (' name ' => ' Pierre ', ' salt ' => 215863)
);

for ($i = 0, $size = sizeof ($people); $i < $size; + + $i)
{
$people [$i] [' salt '] = rand (000000, 999999);
}
?>

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.