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 Pattern of 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

6 Comments
  1. Dheeraj Kumar says

    This is nice diamond program to php ….
    I am sure you would help to every one to the programming….
    I need to add delete search update ,
    And different type of pattern program in php,
    And more which you have updated and new program………

    1. Vikas Kumar says

      Hello Dheeraj Kumar,

      Thanks you like our blog post and yes sure i will add soon PHP pattern program.

      Thanks

  2. Mark Collamar says

    Another way but same output.

    <?php
    $n = 8;

    if($n === 1){ die("input must be greater than 1"); }

    $nn = ($n * 2);
    $m = (ceil($nn / 2) + 1);
    $temp = 0;

    for($x = 1; $x <= $nn; $x++){
    $temp = ($x 1 ? ((2 * $temp) – 1) : $temp;

    echo str_repeat(‘*  ‘, $y) . ”;
    }
    ?>

  3. Mark Collamar says

    <?php
    $n = 8;

    if($n === 1){ die("input must be greater than 1"); }

    $nn = ($n * 2);
    $m = (ceil($nn / 2) + 1);
    $temp = 0;

    for($x = 1; $x <= $nn; $x++){
    $temp = (($x 1 ? ((2 * $temp) – 1) : $temp);

    echo nl2br(str_repeat(‘*  ‘, $y) . “\r\n”);
    }
    ?>

  4. Mark Collamar says

    <?php
    $n = 8;

    if($n === 1){ die("input must be greater than 1"); }

    $nn = ($n * 2);
    $m = (ceil($nn / 2) + 1);
    $temp = 0;

    for($x = 1; $x <= $nn; $x++){
    $temp = (($x 1 ? ((2 * $temp) – 1) : $temp);

    echo nl2br(str_repeat(‘*  ‘, $total) . “\r\n”);
    }
    ?>

  5. Mark Collamar says

    The line of code below must be placed before the echo; I keep posting but the line was removed. so I just comment it below hopefully the admin can add it. Thanks

    $total = ($temp > 1 ? ((2 * $temp) – 1) : $temp);

Leave A Reply

Your email address will not be published.