The Code Developer

PHP program to print Diamond Pattern

In this article write a PHP program to print Diamond Pattern using asterisk (*) symbol. We run for loop 8 times, in which you have two outer for loops which contains two nested for loops to print Pyramid Pattern and Reverse Pyramid Patternof stars to print Diamond Star Pattern of stars as shown in the following program.

Program to print Diamond Pattern

<?php
echo "<pre>";
for ($i = 1; $i < 8; $i++) {
    for ($j = $i; $j < 8; $j++)
        echo "&nbsp;&nbsp;";
    for ($j = 2 * $i - 1; $j > 0; $j--)
        echo ("&nbsp;*");
    echo "<br>";
}
$n = 8;
for ($i = 8; $i > 0; $i--) {
    for ($j = $n - $i; $j > 0; $j--)
        echo "&nbsp;&nbsp;";
    for ($j = 2 * $i - 1; $j > 0; $j--)
        echo ("&nbsp;*");
    echo "<br>";
}
echo "</pre>";
?>

Output:

diamond pattern