star pattern in php

Today, We want to share with you star pattern in php.In this post we will show you pyramid program in php using while loop, hear for reverse pyramid program in php we will give you demo and example for implement.In this post, we will learn about Print Star Pattern In PHP with an example.

Print star pattern in PHP

<?php	
for ($ct=1; $ct<=5; $ct++)	
{	 
	for($pd=1;$pd<=$ct;$pd++)	  
	{	  	
		echo $pd." ";	 
	}	  	
	echo "<br/>";   	
}  
?>

results
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Pattern 2

<?php	
for ($ct=1; $ct<=5; $ct++)	
{	 
for($pd=1;$pd<=$ct;$pd++)	  
{	  	
echo $ct." ";	 
}	  	
echo "<br/>";   	
}  
?>

results
Output
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Pattern 3

<?php	
for ($ct=1; $ct<=5; $ct++)	
{	 
for($pd=1;$pd<=$ct;$pd++)	  
{	  	
echo " * ";	 
}	  	
echo "<br/>";   	
}  
?>

results
Output
*
* *
* * *
* * * *
* * * * *

Pattern 4

<?php	
for ($ct=1; $ct<=5; $ct++) 	       
{ 	 
for ($k=5; $k>$ct; $k--)	 
{	  
//print one space throgh html ;
echo " ";	  
}	
for($pd=1;$pd<=$ct;$pd++)	  
{	  	
echo "*";	  
}	  	
echo "<br/>";	
} 
?>

results
Output
     *
    * *
   * * *
  * * * *
 * * * * *

Pattern 5

<?php  
for($ct=0;$ct<=5;$ct++)
{  
for($pd=5-$ct;$pd>=1;$pd--)
{  
echo "* ";  
}  
echo "<br>";  
}  
?>  

results
Output
* * * * *
* * * *
* * *
* *
*

Pattern 6

<?php  
for($ct=0;$ct<=5;$ct++)
{  
for($k=5;$k>=$ct;$k--)
{  
echo "  ";  
}  
for($pd=1;$pd<=$ct;$pd++)
{  
echo "*  ";  
}  
echo "<br>";  
}  
for($ct=4;$ct>=1;$ct--)
{  
for($k=5;$k>=$ct;$k--)
{  
echo "   ";  
}  
for($pd=1;$pd<=$ct;$pd++)
{  
echo "*  ";  
}  
echo "<br>";  
}  
?>  

Pattern 7

<?php  
for($ct=1; $ct<=5; $ct++)
{   
for($pd=1; $pd<=$ct; $pd++)
{   
echo ' * ';   
}  
echo '<br>';   
}  
for($ct=5; $ct>=1; $ct--)
{   
for($pd=1; $pd<=$ct; $pd++)
{  
echo ' * ';   
}   
echo '<br>';   
}   
?>  

Pattern 8

<?php  
for($ct=5; $ct>=1; $ct--)  
{  
if($ct%2 != 0)  
{  
for($pd=5; $pd>=$ct; $pd--)  
{  
echo "* ";  
}  
echo "<br>";  
}  
}  
for($ct=2; $ct<=5; $ct++)  
{  
 if($ct%2 != 0)  
{  
 for($pd=5; $pd>=$ct; $pd--)  
{  
echo "* ";  
}  
echo "<br>";  
}  
}  
?>  

Pattern 9

<?php
for ($row=1; $row<=3; $row++)
	{
	for ($column=1; $column<=3; $column++)
	{
	echo  $row*$column." ";
	}
	echo "<br>";

}
?>

results

Read Also:  PHP Laravel 6 Multiple Database Connections Tutorial

Output :
1 2 3
2 4 6
3 6 9

Number Pattern

<?php 
function numpat($n) 
{ 
	
	$num = 1; 

	for ($ct = 0; $ct < $n; $ct++) 
	{ 

		for ($pd = 0; $pd <= $ct; $pd++ ) 
		{ 
			
			echo $num." "; 
		} 
		 
			$num = $num + 1; 
		echo "\n"; 
	} 
} 

	// Driver Code 
	$n = 5; 
	numpat($n); 

?> 


results
Output
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 

Star Triangle Pattern 2

<?php 
 
function triangle_pattern($len){ 
$string="*"; 
$start_triangle=""; 
$mid_point=ceil($len/2); 
for($ct=1;$ct<=$mid_point;$ct++){ 
	for($pd = 1; $pd <= $ct; ++$pd) { 
		$start_triangle.=$string." "; 
	} 
	$start_triangle.="\r\n"; 
} 

for($ct=$mid_point;$ct>=1;$ct--){ 
	for($pd = 1; $pd < $ct; ++$pd) { 
		$start_triangle.=$string." "; 
	} 
	$start_triangle.="\r\n"; 
} 

return $start_triangle; 
} 
echo triangle_pattern(9); 
?> 


results
Output
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

PHP programs for printing pyramid patterns

<?php 
function pypart($n) 
{ 
	
	for ($ct = 0; $ct < $n; $ct++) 
	{ 
		 
		for($pd = 0; $pd <= $ct; $pd++ ) 
		{ 
			
			echo "* "; 
		} 
		echo "\n"; 
	} 
} 

	$n = 5; 
	pypart($n); 
?> 


results
Output
* 
* * 
* * * 
* * * * 
* * * * * 

I hope you get an idea about star pattern 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.