To print a pyramid pattern in PHP, you can use nested loops where the outer loop controls the rows and the inner loops control the spaces and stars printed in each row.
In this example,
printPyramid
that takes a parameter $n
to represent the number of rows in the pyramid.echo
to move to the next line.<?php
function printPyramid($n) {
for ($i = 1; $i <= $n; $i++) {
echo str_repeat(' ', $n - $i);
echo str_repeat('*', 2 * $i - 1);
echo "\n";
}
}
printPyramid(5);
?>
* *** ***** ******* *********
In this tutorial, we learned How to print Pyramid Pattern in PHP language with well detailed examples.