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 " "; for ($j = 2 * $i - 1; $j > 0; $j--) echo (" *"); echo "<br>"; } $n = 8; for ($i = 8; $i > 0; $i--) { for ($j = $n - $i; $j > 0; $j--) echo " "; for ($j = 2 * $i - 1; $j > 0; $j--) echo (" *"); echo "<br>"; } echo "</pre>"; ?>
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………
Hello Dheeraj Kumar,
Thanks you like our blog post and yes sure i will add soon PHP pattern program.
Thanks
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) . ”;
}
?>
<?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”);
}
?>
<?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”);
}
?>
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);