Getting Started tutorial for loop statements

Source: Internet
Author: User
Tags php tutorial

For use for loop statements, you know how many times you want to execute a list of statements or statements. This

The For loop is called a clear loop. The syntax in the loop is somewhat complicated, but the loop is often easier than the loop in the

。 The syntax for the For loop is as follows:

for (initialization; condition; increment)
{
code to is executed;
}
The For statement is separated in 3 brackets, and the colon expression is used. When the For loop executes, the following occurs:

The execution of an initialization expression. This expression typically initializes one or more loop counters, but the syntax allows either

How complex the expression is.
The conditional expression is evaluated. If the condition value is true, loop the statement. If the value of the condition is false, the end of the loop

Check.
The expression for this update is executed incrementally.
The execution of the statement, the control returns to step 2.
There is a very simple example of printing out from 0 to 10 numbers:

<?php Tutorial
for ($i =0; $i <=; $i + +)
{
echo "The number is". $i. " <br/> ";
}
?>

The next example is generated to 9 multiplication table 2. The outer loop is responsible for generating a list of dividends, the inner loop will be responsible for producing each number of points

List of frequency converters:

<?php
echo "echo "<table border=2 width=50%";

for ($i = 1; $i <= 9; $i + +) {//this is the outer loop
echo "<tr>";
echo "<td>". $i. " </td> ";

for ($j = 2; $j <= 9; $j + +) {//Inner loop
echo "<td>" $i * $j. " </td> ";
}

echo "</tr>";
}

echo "</table>";
?>

Finally, let's take a look at this example, which uses 2 variables. One to 1 added to all the numbers are 10. The other one adds

into even

<?php
$total = 0;
$even = 0;

for ($x = 1, $y = 1; $x <= $x + +, $y + +) {
if (($y% 2) = = 0) {
$even = $even + $y;
}
$total = $total + $x;
}

echo "The total sum:". $total. " <br/> ";
echo "The sum of even values:". $even;

?>

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.