To print a hollow pyramid pattern in PHP, you can use nested loops. The outer loop controls the rows, and the inner loops manage the spaces and stars to create the hollow effect.
In this example,
<?php
function printHollowPyramid($rows) {
for ($i = 1; $i <= $rows; $i++) {
$pattern = '';
for ($j = $i; $j < $rows; $j++) {
$pattern .= ' ';
}
for ($k = 1; $k < (2 * $i); $k++) {
if ($k === 1 || $k === (2 * $i - 1) || $i === $rows) {
$pattern .= '* ';
} else {
$pattern .= ' ';
}
}
echo $pattern . "\n";
}
}
printHollowPyramid(5);
?>
* * * * * * * * * * * * * * * *
In this tutorial, we learned How to print Hollow Pyramid Pattern in PHP language with well detailed examples.