Print Hollow Diamond Pattern in PHP
ADVERTISEMENTS
Print hollow diamond pattern in PHP. In this program, you will learn how to print hollow diamond patterns in php language.
Source Code
<?php
// Print Hollow Diamond Pattern in PHP
// It's the size of pattern
$x = 15;
// Size of the hollow diamond should be even number
if ($x % 2 == 1)
$x++;
// This will print the hollow diamond pattern
hollowDiamond($x);
function hollowDiamond($size) {
$diff = $size / 2;
echo "-----The hollow diamond pattern is-----\n\n";
// This will print the first half diamond
for ($i = 1; $i <= $diff; $i++) {
echo "\t";
for ($j = 1; $j <= $diff - $i; $j++) {
echo " ";
}
if ($i == 1) {
echo "*";
} else {
echo "*";
for ($j = 1; $j <= 2 * $i - 3; $j++) {
echo " ";
}
echo "*";
}
echo "\n";
}
// This will print the last half diamond
for ($i = $diff + 1; $i < $size; $i++) {
echo "\t";
for ($j = 1; $j <= $i - $diff; $j++) {
echo " ";
}
if ($i == $size - 1) {
echo "*";
} else {
echo "*";
for ($j = 1; $j <= 2 * ($size - $i) - 3; $j++) {
echo " ";
}
echo "*";
}
echo "\n";
}
}
?>
Output
-----The hollow diamond pattern is-----
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*