PHP For Loop Learning notes 99 multiplication table

Source: Internet
Author: User

For statement
You can use a For statement if you have determined how many times the code block has been repeatedly executed.

Grammar

Php

The code is as follows Copy Code
for (initialization; condition; increment)
{
code to is executed;
}

Note: The For statement has three parameters. The first parameter initializes the variable, the second parameter holds the condition, and the third parameter contains the increment required to perform the loop. If you include multiple variables in the initialization or increment parameter, you need to separate them with commas. The condition must evaluate to TRUE or false.

Example
The following example displays the text "Hello world!" 5 times:

Php

The code is as follows Copy Code


<body>

<?php
For ($i =1 $i <=5; $i + +)
{
echo "Hello world!<br/>";
}
?>

</body>

Let's take a look at the positive sequence of the 99 multiplication table to use nested loops.

The code is as follows Copy Code
<?php
/**
99 positive sequence of multiplication table
**/
For ($i =1 $i <=9; $i + +)
{
echo "<br>";
For ($n =1 $n <=9; $n + +)
{
if ($i >= $n)
{
echo "$i * $n". = '. $i * $n;
echo "&AMP;NBSP";
}
}
}
?>

The results of running this PHP:

The following wants to make this multiplication table reverse, in fact, only for the loop within the number of gradually diminishing.

The code is as follows Copy Code

<?php
/**
99 the reverse of the multiplication table
**/
for ($i =9; $i >=1; $i-)
{
echo "<br>";
for ($n =9; $n >=1; $n-)
{
if ($i >= $n)
{
echo "$i * $n". = '. $i * $n;
echo "&AMP;NBSP";
}
}
}
?>

The reverse display is as follows:

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.