Problems with multiplication tables in PHP

1

I'm doing some multiplication tables I've already done one with for worst I do with while and do while but I do not get the same result the for is this

<?php

for($t=8; $t<=10; $t++)
{

 echo "<h3> Tabla del $t </h3>";
 // generamos la tabla
 for($i=8; $i<=12; $i++)
 {
  echo "$t x $i = ".($t*$i) . "<br/>";
 }
}

?>

With while worse the same thing does not come out

  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

<?php
$t=8;
$i=8;
while($t<=10){
    echo "<h3> Tabla del $t </h3>";
    $t++;
    while($i<=12){
        echo "$t x $i = ".($t*$i) . "<br/>";
        $i++;
    }
}

What can be my mistake?

    
asked by wdwd 03.10.2017 в 19:21
source

2 answers

0

Two point errors in your code

  • The increase of $t should be after the second while if it would not start from 9
  • The variable% co_of% after the second% co_of% remains at 13, then you should reset its value to% co_of% if you no longer enter the cycle of the second% co_of%

    $t=8;
    $i=8;
    while($t<=10){
       echo "<h3> Tabla del $t </h3>";
       while($i<=12){
          echo "$t x $i = ".($t*$i) . "<br/>";
          $i++;
       }
     $i=8; //Reset variable
     $t++; //Incremento
    }
    
answered by 03.10.2017 / 19:29
source
1

You have a logic error when applying while .

For each round of the for , the first thing it does is initialize the counter variable and then start iterating ..

So internally, the for does something like this:

$i=8;

Your second while , does not. The first time i has the correct value, but then you never return to the source value, and there is the error.

You must move the instruction:

$i=8;

Before

while($i<=12){

and your problem will be resolved.

    
answered by 03.10.2017 в 19:31