skip to Main Content

I’m trying to create a multiplication table using PHP but in my code the number 1 is skipped i don’t know why.
Can you please guys help me to solve this problem.
Thanks a lot.

<?php
$codeHTML = '<html>
<head>
  <meta charset="utf-8">

  <title>Programmation pour le Web</title>

  <link rel="stylesheet" href="style.css">
  <script src="script.js"></script>
</head>';
$codeHTML .= '<body><h1> Table de multiplication </h1>
<table border = "2" width = "100">';
// Creation de la table
for($i = 1; $i < 6; $i++){
  $codeHTML .= '<tr>';
  for($j = 1; $j < 6; $j++){
    $p = $i*$j;
    $codeHTML .= "<td> $p </td>";
  }
  $codeHTML .= '</tr>';
}
$codeHTML .= '</table>
</body>
</html>';
echo $codeHTML;
?>

And i want an output to be like :

x  1  2  3  4  5
1  1  2  3  4  5
2  2  4  6  8  10
3  3  6  9  12 15
4  4  8  12 16 20
5  5  10 15 20 25

2

Answers


  1. When you provide expected outpus is much easier to help you, here is what you want:

    <?php
    $codeHTML = '<html>
    <head>
      <meta charset="utf-8">
      <title>Programmation pour le Web</title>
      <link rel="stylesheet" href="style.css">
      <script src="script.js"></script>
    </head>';
    
    $codeHTML .= '<body><h1> Table de multiplication </h1>
    <table border = "2" width = "100">';
    
    $codeHTML .= "<tr> <td>x</td>";
    for($i = 1; $i <= 6; $i++) {
      $codeHTML .= "<td>$i</td>";
    }
    $codeHTML .= '</tr>';
    
    for($i = 1; $i <= 6; $i++){
      $codeHTML .= '<tr>';
      $codeHTML .= "<td>$i</td>";
      for($j = 1; $j <= 6; $j++){
        $p = $i*$j;
        $codeHTML .= "<td> $p </td>";
      }
      $codeHTML .= '</tr>';
    }
    $codeHTML .= '</table>
    </body>
    </html>';
    echo $codeHTML;
    ?>
    

    Output is:

    x   1   2   3   4   5   6
    1   1   2   3   4   5   6
    2   2   4   6   8   10  12
    3   3   6   9   12  15  18
    4   4   8   12  16  20  24
    5   5   10  15  20  25  30
    6   6   12  18  24  30  36
    
    Login or Signup to reply.
  2. Is this something you want ?

    <?php
    $codeHTML    = '<html>
    <head>
      <meta charset="utf-8">
    
      <title>Programmation pour le Web</title>
    
      <link rel="stylesheet" href="style.css">
      <script src="script.js"></script>
    </head>';
    $codeHTML    .= '<body><h1> Table de multiplication </h1>
    <table border = "2" width = "100">';
    
    for ( $i = 0; $i < 6; $i ++ ) {
        $codeHTML .= '<tr>';
        for ( $j = 0; $j < 6; $j ++ ) {
            $p = $i * $j;
            if ( 0 == $i && 0 == $j ) {
                $p = 'x';
            } elseif ( $j == 0 ) {
                $p = $i;
            } elseif ( $i == 0 ) {
                $p = $j;
            }
            $codeHTML .= "<td> $p </td>";
        }
        $codeHTML .= '</tr>';
    }
    
    $codeHTML .= '</table>
    </body></html>';
    
    echo $codeHTML;
    

    This will output:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search