The star triangle in PHP is made using for and foreach loop. There are a lot of star patterns. We'll show some of them here.
Pattern 1
<?php for($i=0;$i<=5;$i++){ for($j=5-$i;$j>=1;$j--){ echo "* "; } echo "<br>"; } ?>
Output:
Pattern 2
<?php for($i=0;$i<=5;$i++){ for($j=1;$j<=$i;$j++){ echo "* "; } echo "<br>"; } ?>
Pattern 3
<?php for($i=0;$i<=5;$i++){ for($k=5;$k>=$i;$k--){ echo " "; } for($j=1;$j<=$i;$j++){ echo "* "; } echo "<br>"; } for($i=4;$i>=1;$i--){ for($k=5;$k>=$i;$k--){ echo " "; } for($j=1;$j<=$i;$j++){ echo "* "; } echo "<br>"; } ?>
Pattern 4
<?php for($i=1; $i<=5; $i++){ for($j=1; $j<=$i; $j++){ echo ' * '; } echo '<br>'; } for($i=5; $i>=1; $i--){ for($j=1; $j<=$i; $j++){ echo ' * '; } echo '<br>'; } ?>
Pattern 5
<?php for ($i=1; $i<=5; $i++) { for ($j=1; $j<=5; $j++) { echo '* '; } echo "</br>"; } ?>
Pattern 6
<?php for($i=5; $i>=1; $i--) { if($i%2 != 0) { for($j=5; $j>=$i; $j--) { echo "* "; } echo "<br>"; } } for($i=2; $i<=5; $i++) { if($i%2 != 0) { for($j=5; $j>=$i; $j--) { echo "* "; } echo "<br>"; } } ?>
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Pattern 1
Output:
Pattern 2
Output:
Pattern 3
Output:
Pattern 4
Output:
Pattern 5
Output:
Pattern 6
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer