Number pattern programs in php Examples

Today, We want to share with you number pattern programs in php.In this post we will show you number pyramid program in php, hear for star pattern in php we will give you demo and example for implement.In this post, we will learn about PHP Nested Forloop – Exercises Pyramid Program Solution with an example.

number pyramid program in php

Number pattern triangle in PHP can be printed using for and foreach loop. There are a some of patterns in number pattern. Some of them are display here.

Read Also:  php ajax post - jQuery AJAX Post Example with PHP and JSON

Number Triangle

Example 1:

<?php  
$k=1;  
for($no=0;$no<4;$no++){  
for($x=0;$x<=$no;$x++){  
echo $k." ";  
$k++;  
}  
echo "<br>";  
}  
?>  

Example 2: PHP program to print the number pattern
To print the number pattern in PHP

<?php
function num_pattern($val)
{
   $num = 1;
   for ($m = 0; $m < $val; $m++)
   {
      for ($n = 0; $n <= $m; $n++ )
      {
         echo $num." ";
      }
      $num = $num + 1;
      echo "\n";
   }
}
$val = 6;
num_pattern($val);
?>

Example 3: Program to print Triangle number in reverse pattern

<?php
for($no=6;$no>=1;$no--)
 {
   for($x=$no;$x>=1;$x--)
    {
     echo $x." ";
     if($x==1)
     {
       echo " ";
       echo "<br />";
     }
   }
} 
?>

Results:
6 5 4 3 2 1 
5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1

Program to print Triangle number in reverse pattern

<?php 
for($no=1;$no<=6;$no++)
{
for($x=$no;$x>=1;$x--)
 { echo $x." ";
   if($x==1)
     {
       echo " ";
       echo "<br />";
     }
  }
 } 
?>

Results:

1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1 
6 5 4 3 2 1

I hope you get an idea about PHP Number Triangle Program(number pattern programs in php).
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.