search

Home  >  Q&A  >  body text

Questions about multiplication table for for loop

Could you please give me a detailed explanation of how the multiplication table is calculated? I really can’t figure it out. It’s going to explode.╮(╯▽╰)╭

for ($i = 1 ; $i < 10 ; $i ){

for ( $j=1; $j<=$i; $j ){

echo $j.'x'.'$i'.'='.($i*$j).'';

}

echo '<br />';

}


我只是一条咸鱼哈我只是一条咸鱼哈2639 days ago1608

reply all(2)I'll reply

  • 清楚叛逆者

    清楚叛逆者2017-09-02 09:20:34

    <?php
    //循环9次,表示9列,如同:1*1=1的1,2*1=2的2
    for($i=1;$i<10;$i++){
        //上面1次循环在这个循环9次,表示$i行如同:1*1=1的1,1*2=2的1和2
        for($k=1;$k<$i;$k++){
            //输出如同:2*2=4;&nbsp;表示空格也可以用\t代替
            echo $k."*".$i."=".$i*$k."&nbsp;&nbsp;&nbsp;";
        }
        //每一次次循环换行
        echo "<br />";
    }


    reply
    1
  • 我只是一条咸鱼哈

    Thank you

    我只是一条咸鱼哈 · 2017-09-02 11:49:28
  • ringa_lee

    ringa_lee2017-09-02 09:05:58

    The loop is easy to understand. You can bring the values ​​in step by step to see what the result of the operation is. Look from the inside out, for example, starting from the first for loop outside:

    for ($i = 1 ; $i < 10 ; $i++){
        //第一次循环
        $i=1;
        for ( $j=1; $j<=$i; $j++){
        //第一次循环
        $j=1;
            echo $j.'x'.'$i'.'='.($i*$j).'';
            //这个echo 结果为:1 x 1 = 1;依次类推
            //里面的for第二次循环的时候就是$i=1;$j=2;这个echo 结果为:1 x 2 = 2;
            //里面的这个for循环结束,再从外面的for循环开始当$i=2,3,4,5....
    }
        echo '<br />';
    }


    reply
    0
  • 我只是一条咸鱼哈

    Thank you (^o^)/~ I entered the values ​​one by one, and I suddenly had an epiphany O(∩_∩)O~

    我只是一条咸鱼哈 · 2017-09-02 11:48:29
  • Cancelreply