Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Diamond Shape Program Code in PHP

Diamond Shape Program Code in PHP

In this tutorial, we learn the code of diamond shape program in PHP.

Diamond Shape Program Code in PHP

<html>
    <head>
        <title>Diamond Shape!</title>
       
    </head>
	<body>
	<?php

 
    $limit = 10;
    $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>";
    }

?>
	</body>
</html>

Diamond Shape in PHP with Form Values entered by the user

In this tutorial, we learn the code of diamond shape program in PHP with form values entered by the user.

<html>
    <head>
        <title>print Diamond Shape!</title>     
    </head>
    <body>
        <h2>print Diamond Shape!</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 
$con = mysqli_connect("localhost","root","","mydatabase");
//echo $con;
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
if (isset($_POST['print']) && $_POST['print']) {
 
    $limit = $_POST['limit'];
	$sql = "INSERT INTO diamond (DiamondValue) VALUES ($limit)";
	if ($con->query($sql) === TRUE) {
    echo "New record created successfully";
	echo "<br>";
} else {
    echo "Error: " . $sql . "<br>" . $con->error;
}
    $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

Diamond Shape Program Code in PHP

Figure: Diamond Shape Program Code in PHP

Database View for Diamond Shape Program

Diamond Shape in PHP with Form Values entered by the user

Figure: Diamond Shape in PHP with Form Values entered by the user

Fazal Rehman Shamil Click Here to Know More
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325
Dear Professors and Resarchers!You are welome to Cite these tutorials in your research or slides etc. Please don't forget to mention the reference of website. Copy Paste of text is strcitly forbidden. Images can be reuse because images are protected with watermark.