happy cat image

Start Web Coding

Your First step toward web coding,
Start Web Coding

change UI

for - loop

The loop statement is used to calculate the cumulative sum from 1 to 100 or to output the multiplication table from 2 to 9.
Types include for statement, while statement, and do-while statement.
Let's look at the for statement To know the for statement, it is easy to understand how to implement a source that prints 1 to 10.
Of course, there are many cases when you go to practice. The for statement is used a lot. You should know well ~ !! If you want to write 1 to 10 line by line, you should do as follows.

<?php
    echo "1 <br />";
    echo "2 <br />";
    echo "3 <br />";
    echo "4 <br />";
    echo "5 <br />";
    echo "6 <br />";
    echo "7 <br />";
    echo "8 <br />";
    echo "9 <br />";
    echo "10 <br />";
?>
php for image

You have to type Using the for statement can greatly reduce the source.

<?php
    for($a = 1; $a <= 10; $a++) {
        echo " $a <br />";
    }
?>
php for image

You can do it with 3 lines as above.
Then shall we look at the process?

<?php
    for($a = 1; $a <= 10; $a++){
      echo " $a <br />";
    }
?>

for($a = 1; $a <= 10; $a ++) Here you see three statements. In order(variable declaration, condition, increment)
Declare variable $a = 1. That is, assign 1 to a
If so, the condition $a <= 10 distinguishes between true and false.
If true
echo "$a <br />"; Run
Run $a ++ after the run.
If you run $a ++, a is assigned to 2.
Then the
In the condition $a <= 10, distinguish between true and false again. Before it was true because it was 1, and now it is true because it is 2.
Execute the echo statement. Then run $a ++ again to determine whether the condition is true or false. Like this
Repeatedly, if 11 is assigned to a, it is determined to be false and the echo statement is not executed anymore.
So let's get the cumulative values ​​from 1 to 10.

<?php
    echo "Get accumulated value using for statement from 1 to 10 <br />";
    echo "=============================================== <br />";

    $sum = 0; // declare to store cumulative value here

    for($a = 1; $a <=10; $a++){
        $sum += $a;
        echo "The cumulative sum up to {$a} is {$sum}. ^^ <br />";
    }
?>
php image

Since we need something to store the accumulated values, we declare $sum and store the accumulated values ​​there.
If so

for($a = 1; $a <=10; $a++)
{
$sum += $a;
echo "The cumulative sum up to {$a} is {$sum}. ^^ <br />";



Since a is 1 and 1 is less than 10, it is determined to be true in the conditional expression.
Since it is determined to be true, execute the following command.
$sum was 0, but by adding + = $a; to 1, the $sum value is 1.
Then run the echo statement.
After that, the increment is performed. Since $a ++, $a becomes 2,
Is less than 10 under the condition
$sum + = $a; Perform Since $sum is 1 and $a is 2, it becomes 1 + 2, so $sum is 3.
After that, the echo statement is executed again and then incremental again. Then $a becomes 3, and if the condition is true all the time, it repeats 11
When it stops running. Because it was determined to be false ...

Double for statement

A double for statement declares another for statement within the for statement.
There is no need to think hard. It's just literally a for statement.
For example
If you make only the numbers 3 to 2 and 3

<?php
    for($a = 2; $a <= 3 ; $a++){

        echo "multiplication table $a column <br />";

        for($b = 1; $b <= 3 ; $b ++){
            $mul = $a * $b;
            echo " $a * $b = $mul <br />";
        }
    }
?>
php image

Have you declared the for statement? The reason for starting from 2 is because it is from 2nd.
Since it is less than 3, the condition is true to execute the next execution statement.
Then the multiplication table 2nd stage is printed out and the next run
Next is the for statement. Therefore, execute the for statement.
b = 1; b <= 3; b ++
and declare a variable mul to hold the product of a and b.
Then execute the last statement, echo "$a * $b = $mul.
Then
a is 2 b is 1, so increase
2 * 1 = 2($mul)
b and convert it to 2 so that the condition is true. After that,
the echo statement is executed again, so
2 * 2 = 4
is executed, and then again b is incremented to 3, which is the same as 3, which again satisfies true
and represents 2 * 3 = 6 again, increasing to 4 again.
4 is determined to be false to exit the for statement
then because to go in for statements that were initially came to run the executable statement to execute the first for statement $a ++ a is 3.
3, so is indeed a distinction was that
Run the following statement again.
After outputting the multiplication table 3rd stage,
the for statement below is executed, and the
3rd stage is expressed by the above process, and when
it exits, the value of a becomes 4 and it is determined to be false and ends.

I hope the description is well conveyed. ;;

In this way you can create as many tables as you want.
You can multiply 19 up to 19 levels and as many times as you can.
So how do you print the multiplication tables up to the normal 9th ​​stage?
Change the condition of the first for statement that sets up the first multiplication table to 9 instead of 3, and change the
condition of the second for statement to 9.
As follows

<?php
    for($a = 2; $a <= 9 ; $a++){

      echo "multiplication table $a column <br />";

      for($b = 1; $b <= 9 ; $b ++){
        $mul = $a * $b;
        echo " $a * $b = $mul <br />";
      }
    }
?>

result

php image

강좌로 돌아가기