Purpose: if $ r = 10, builds a 10x10 table, if $ r = 100 ...

What came up with:

$r=10; echo '<TABLE>'; $x=1; while ($x<=$r) { echo '<TR>'; while ($x<=$r) { echo '<TD>1</TD>'; $x++; } echo '</TR>'; $x++; } echo '</TABLE>'; 

But only the first line is built. Where is the mistake?

  • 3
    Getting into the first cycle $ x = 1, then we get into the second cycle. The variable $ x increases to 10. We exit from the second cycle to the first one already with the value $ x = 10. We finish the first cycle and we already have $ x = 11 - Demyan112rv
  • Yes, I already realized this blunder - Andrey Stifurak

3 answers 3

Damn guys, what kind of shit?

 while ($x<=$r) { // раз $x<=$r echo '<TR>'; while ($x<=$r) { // два $x<=$r echo '<TD>1</TD>'; $x++; 

Those. it builds one line correctly, because then it returns to the first cycle and the condition is not met, and the cycle ends. Change the variables in the second cycle

 $r=10; $x=1; echo '<TABLE>'; while ($x<=$r) { echo '<TR>'; while ($xx<=$r) { echo '<TD>1</TD>'; $xx++; } echo '</TR>'; $x++; } echo '</TABLE>'; 

Suppose so. And happiness is guaranteed!

     $r=10; echo '<TABLE>'; $x=1; $y=1; while ($x<=$r) { echo '<TR>'; while ($y<=$r) { echo '<TD>1</TD>'; $y++; } echo '</TR>'; $x++; } echo '</TABLE>'; 
    • works, thank you - Andrew Stifurak

    Less cycles - less flexibility, but an empty table can be created.

     $r = 10; $tds = str_repeat('<td> поле </td>', $r); $trs = str_repeat("<tr> $tds </tr>", $r); $table = "<table> $trs </table>"; echo $table; 
    • thanks, so much more careful - Andrew Stifurak
    • use) - xEdelweiss