To print a hollow inverted pyramid pattern in PHP, we use nested loops to manage spaces and stars.
In this example,
<?php
function printHollowInvertedPyramid($numRows) {
for ($row = $numRows; $row >= 1; $row--) {
$pattern = '';
for ($space = 0; $space < $numRows - $row; $space++) {
$pattern .= ' ';
}
if ($row == $numRows || $row == 1) {
$pattern .= str_repeat('*', 2 * $row - 1);
} else {
$pattern .= '*';
$pattern .= str_repeat(' ', 2 * $row - 3);
$pattern .= '*';
}
echo $pattern . "\n";
}
}
printHollowInvertedPyramid(5);
?>
********* * * * * * * *
In this tutorial, we learned How to print Hollow Inverted Pyramid Pattern in PHP language with well detailed examples.