PHP Script to print Diamond Star Pattern!

Hi Students, let’s have another program for you!. Print Diamond star pattern using PHP Script.

See the demo here:

demo

<html>
    <head>
        <title>PHP Script to print Diamond pattern!</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <h2>PHP Script to print Diamond pattern!</h2>
        <form method="post">
            Enter the limit:  <input type="number" name="limit" min="2" max="100" />&nbsp;<input type="submit" value="Print Diamond Pattern!" name="print" />
        </form>
    </body>
</html>

<?php
//check if the form is submitted 
if (isset($_POST['print']) && $_POST['print']) {

    $limit = $_POST['limit'];
    $space = $limit;

    for ($i = 1; $i <= $limit; $i++) {
        for ($j = 1; $j <= $space; $j++) {
            echo "&nbsp;&nbsp;";
        }
        $space--;

        for ($j = 1; $j <= 2 * $i - 1; $j++) {
            echo "*";
        }

        echo "<br>";
    }

    $space = 2;

    for ($i = 1; $i <= $limit; $i++) {
        for ($j = 1; $j <= $space; $j++) {
            echo "&nbsp;&nbsp;";
        }
        $space++;

        for ($j = 1; $j <= 2 * ($limit - $i) - 1; $j++) {
            echo "*";
        }

        echo "<br>";
    }
}
?>

Output of the above program will be something like this:
diamond-pattern-php

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top