PHP program to print a pattern of pyramid



Let us see an example to print a pattern of pyramid in PHP βˆ’

Example

 Live Demo

<?php
function print_pattern($val)
{
   $num = 2 * $val - 2;
   for ($i = 0; $i < $val; $i++)
   {
      for ($j = 0; $j < $num; $j++)
      echo " ";
      $num = $num - 1;
      for ($j = 0; $j <= $i; $j++ )
      {
         echo "* ";
      }
      echo "
";    } } $val = 7; print_pattern($val); ?>

Output

         *
        * *
       * * *
      * * * *
     * * * * *
    * * * * * *
   * * * * * * *

A function named β€˜print_pattern’ is defined that iterates through the number of rows through which the pattern needs to be generated. Relevant line breaks are also echoed using the β€˜echo’ attribute. The function is called by passing the number of rows for which the pattern needs to be generated.

Updated on: 2020-07-02T06:32:21+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements