Problem with program that makes the multiplication table of 5

0

I'm doing a program that makes the multiplication table of 5 but I occupy that I display on the screen so at the time of putting echo does not let me print the characters throws me the numbers of 5 in 5 but does not throw me the characters of the echo are not due to

5 x 1 = 5
5 x 2 = 10
5 x 1 = 15

This is my code:

    <?php
echo"<table BORDER='1'>";

echo"
<tr>
<td colspan=7 align=center> TABLA  DEL 5</td>
</tr> 
 <td> 5 x 1 </td>
<td> 5 x 2</td>
<td> 5 x 3</td>
<td> 5 x 4</td>
<td> 5 x 5</td>
<td> 5 x 6</td>
<td> 5 x 7</td>
<td> 5 x 8</td>
<td> 5 x 9</td>
<td> 5 x 10</td>
</tr>
</table>";



$a=1;
$b=5;
$resultado;
for($i=1; $i<=10; $i++)
{
    $resultado= $i * 5;

     echo "Las tabla de multiplicar del 5 es " $b. ."*".  $a.  ".=."  $resultado;




}


?>
    
asked by wdwd 06.04.2018 в 21:13
source

4 answers

1

To obtain the multiplication table of 5 and have a more ordered code in turn, you should first remove the title of the cycle for if it is not printing in each iteration; I also do not see the need for the variable $ a and finally your for should start from scratch otherwise it's fine; Check it with the example I give you

<?php
echo "Las tabla de multiplicar del 5 es";
$b=5; $resultado;
for($i=0; $i<=10; $i++)
{
  echo $b*$i.PHP_EOL;
}

?>

Now if you want next to each result to appear the operation that is being done so that it has more the multiplication table form, do the following

<?php
echo "Las tabla de multiplicar del 5 es";
$a=1; $b=5; $resultado;
for($i=0; $i<=10; $i++)
{
  echo "$b * $i = ".$b*$i.PHP_EOL;
}

?>
  

The previous way you have the desired result but with a more code   ordered in addition to that with PHP_EOL you have line breaks too;   all that in a cleaner code

    
answered by 06.04.2018 / 21:19
source
1

Correcting your error, your program would be as follows:

The error is in the concatenation of your echo in for

echo"";

echo" TABLA DEL 5 5 x 1 5 x 2 5 x 3 5 x 4 5 x 5 5 x 6 5 x 7 5 x 8 5 x 9 5 x 10 \n\n";

$a=1; $b=5; $resultado; for($i=1; $i<=10; $i++) { 
    $resultado= $i * 5;
    echo "Las tabla de multiplicar del 5 es ". $b ."*". $a .  "=".$resultado."\n";
}
    
answered by 06.04.2018 в 21:24
1

Your code is correct in the following way:

<?php
echo "TABLA DEL 5 5 x 1 5 x 2 5 x 3 5 x 4 5 x 5 5 x 6 5 x 7 5 x 8 5 x 9 5 x 10 <br>";
$b=5; $resultado; 
for($i=1; $i<=10; $i++) { 
 $resultado = $i * $b;
 echo "Las tabla de multiplicar del 5 es ".$b."*".$i." = ".$resultado."<br>";
}
?>

The variable $a disappears since you do not need it, to perform the operations you only need $b and the variable $i that is inside the for and when printing the data replace the variable $a the $i since it is the variable that is increasing in each cycle and finally in the variable $resultado change the number 5 by the variable $b since you were not making use of the variables you had declared.

    
answered by 06.04.2018 в 21:30
1

To generate a table you can do it this way:

<?php
// Ingresa el numero de tabla a multiplicar
$tabla = 5;

echo "<table border=1 cellspacing=0 width=200";
echo "<tr><th colspan=5> Tabla del $tabla </th></tr>";

// Iteracion para generar la Tabla
 for($i=1; $i<=10; $i++) 
 {
   echo "<tr><td align=center>$tabla</td>
             <td align=center>x</td>
             <td align=center>$i</td>
             <td align=center>=</td>
             <td align=center> ". $tabla*$i . "</td>
         </tr>";
 }
 echo "</table> <br/>";
?>

You can test it here

See other examples: Page

    
answered by 06.04.2018 в 22:26