Skip to content
Advertisement

Generate a pyramid using PHP For Loop and HTML

I made a script where it creates a pyramid using a php for loop and html table elements, but the pyramid isn’t how I want it yet. The code:

<?php
echo "<table width=400px";
echo "<tr>";
//Inner Loop
for ($x = 0; $x <= 8; $x++) {
//Outer loop
    for ($z = 1; $z <= $x; $z++) {
        echo "<td height=50px width=50px bgcolor=black></td>";
    }
    echo "n";
    echo "</tr>";
}
echo "</table>";
?>

Right now it looks like this: enter image description here

But I want it to look like this: enter image description here

Can someone help me with that?

Advertisement

Answer

Simply add cellspacing=0 to table

<?php
echo "<table cellspacing=0 width=400px";
echo "<tr>";
//Inner Loop
for ($x = 0; $x <= 8; $x++) {
//Outer loop
    for ($z = 1; $z <= $x; $z++) {
        echo "<td height=50px width=50px bgcolor=black></td>";
    }
    echo "n";
    echo "</tr>";
}
echo "</table>";
?>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement