0

i have a code to create star combined with number, the result that i have like this:

1*****2*****3*****4*****5

and the source code:

<?php
$max = 5;

for ($i=1; $i<=$max; $i++){
    for ($j=1; $j<=$max; $j++){
        if ($i==$j){
            echo "$j";
        }
        else{
            //echo "<br>";
            echo "*";   
        }
    }
}
?>
<br />

and the result that i want like this:

1 * * * * 
* 2 * * * 
* * 3 * * 
* * * 4 *
* * * * 5

How to add a new line on my code??

Alex Andrei
  • 7,315
  • 3
  • 28
  • 42

2 Answers2

1
<?php
$max = 5;
for ($i=1; $i<=$max; $i++) {
    for ($j=1; $j<=$max; $j++)   {
        if ($i==$j)
            echo "$j ";
        else   
            echo "* "; 
    }
    echo "<br/>";
}
?>
Severino Lorilla Jr.
  • 1,637
  • 4
  • 20
  • 33
1

Change you code like this:

$max = 5;
for ($i = 1; $i <= $max; $i++) {
    for ($j = 1; $j <= $max; $j++)
        if ($i == $j) {
            echo "$j";
        } else {
            echo "*";
        }
    echo "\n";
}

Check results here

Ali
  • 1,408
  • 10
  • 17