Table of Contents
PHP Star triangle Pattern program
Here’s a simple Java program that demonstrates how to print star triangle patterns using PHP. The PHP Star pattern is created only using for loop
Program 1
<?php
//Star Pyramid Size
//PHP pyramid patterns
$size = 8;//Rows of Pyramid
for($i=1;$i<=$size;$i++){
for($j=1;$j<=$size-$i;$j++){
print (" ");//print initial spaces
}
for($k=1;$k<=$i;$k++){
print (" ");//print spaces between stars
print ("*");//print stars
}
echo "\n";//move to next line
}
?>
When the above code is executed, it produces the following result

Program 2
<?php
//Star Pyramid Size
//PHP pyramid patterns
$row = 6;//define Rows of Pyramid
for($i=$row; $i>=1; --$i){
for($space=0 ;$space<=$row-$i; ++$space)
echo (" ");//add initial spaces
for($j=$i; $j<=2*$i -1 ;$j++)
print ("* ");//print stars
echo "\n";//move to next line
}
?>
When the above code is executed, it produces the following result

Program 3
<?php
//Star Pyramid pattern
//PHP pyramid patterns
$size = 7;//Rows of Pyramid
for($i=1;$i<=$size;$i++){
for($j=1;$j<=$i; $j++){
echo("*");//print star
}
echo "\n";//move to next line
}
for($i=$size; $i>=1; $i--){
for($j=1; $j<=$i; $j++){
echo("*");//print star
}
echo "\n"; //move to next line
}
When the above code is executed, it produces the following result

Program 4
<?php
//PHP star triangle patterns
$rows = 10;//Rows of triangle
print("Triangle star pattern\n");
for($i=1; $i<=$rows; $i++){
for($spaces=1; $spaces<=$i; $spaces++){
echo (" ");//print initial spaces
}
for($j=$rows-1; $j>=$i ;$j--){
echo ("*");//print stars
}
echo "\n";//move to next line
}
?>
When the above code is executed, it produces the following result

Program 5
<?php
//PHP star triangle patterns
$rows = 10;//Rows of triangle
print("Triangle star pattern\n");
for($i=1; $i<=$rows; ++$i){
for($space=$rows-1 ;$space>=$i; --$space){
echo (" ");//print initial spaces
}
for($j=1; $j<=$i ;$j++){
echo ("*");//print stars
}
echo "\n";//move to next line
}
?>
When the above code is executed, it produces the following result

Program 6
<?php
//PHP star triangle patterns
$rows = 8;//Rows of triangle
print("Triangle star pattern\n");
for($i=0; $i<=$rows; ++$i){
for($j=$rows-$i; $j>=1; --$j){
echo ("*");//print stars
echo (" ");// print spaces between stars
}
echo "\n";//move to next line
}
?>
When the above code is executed, it produces the following result

Program 7
<?php
//PHP star triangle patterns
$rows = 8;//Rows of triangle
print("Triangle star pattern\n");
for($i=1; $i<=$rows; ++$i){
for($j=1; $j<=$i; ++$j){
}
for($k=1;$k<=$i;$k++){
echo ("*");//print stars
echo (" ");// print spaces between stars
}
echo "\n";//move to next line
}
?>
When the above code is executed, it produces the following result

Suggested post
C pyramid star pattern program – using loops
C++ pyramid star pattern program – using loops
Java pyramid star pattern program – using loops
C program to create an Inverted Pyramid star pattern
C++ program to create an Inverted Pyramid star pattern
Hollow Pyramid star pattern in C language
Hollow Pyramid star pattern in C++ language