PHP Pyramid Loop Patterns Tutorial

Here is an example code in PHP for printing a pyramid.

<?php
$rows = 5;

for ($i = 1; $i <= $rows; $i++) {
    // 打印空格
    for ($j = 1; $j <= $rows - $i; $j++) {
        echo " ";
    }

    // 打印星号
    for ($k = 1; $k <= 2 * $i - 1; $k++) {
        echo "*";
    }

    echo "\n";
}
?>

Executing the code above will display a 5-level pyramid shape in the terminal as shown below:

    *
   ***
  *****
 *******
*********
bannerAds