star half pyramid

In this tutorial, i will gonna show you how to make all types pattern in php.

Introduction to Patterns in PHP


What is Pattern Programming in PHP? It is an art of programming to print a type of example on the screen. This can be a progression of numbers, letters, or uncommon characters to form a pattern.

For this article, we will be gonna using PHP to code the patterns. Do not worry about that. Once you get the hang of it, it’s just the syntax that varies from language to language. The logic is the same, always in other codings.

Examples of Pattern in PHP


  • Before go ahead onto the programs, let us first understand the basic logic of coding patterns. Patterns are always programmed through nested loops – mostly nested for loops. This is just because loops are syntactically easier to understand and concise.
  • The outer loop is always concerned with the number of lines. So, let us say if you have to print a pattern spanning five lines, the outer loop will always run five times.
  • The inner loop is always concerned with the number of elements in each line. So, let us say if you have to print 1 star in the first line, 2 stars in the second line and so on, the inner loop would control this logic.
  • Depending up on the patterns, sometimes there are more than one inner loops or three levels of nested loops. And, there is the involvement of spaces and tabs to produce the desired pattern.

So Keep in mind, Let’s try to code the pattern now.

1.Star Half Pyramid.

This is the very simplest pattern to print. It prints the increasing number of starts in subsequent lines. 1 star in the first line, 2 stars in the second line and three star in the third line and so on.

star half pyramid pattern in php

Let us code this pattern for five lines. Keep the logic in mind, our outer loop will run five times. Since the number of stars in each line is directly dependent on the line number, our inner loop will be a function of the control variable in our outer loop. Let us see how.

ber of stars in each line is directly dependent on the line number, our inner loop will be a function of the control variable in our outer loop. Let’s see how.

Our outer control variable is i and inner control variable is j.
Outer loop iteration 1 –> i = 0
Inner loop iteration 1 –> = 0
Print star
Outer loop iteration 2  –> i = 1
Inner loop iteration 1 –> j = 0
Print Star
Inner loop iteration 2 -> j = 1
Print Star
Outer loop iteration 3  –> i = 2
Inner loop iteration 1 –> j = 0
Print Star
Inner loop iteration 2 -> j = 1
Print Star
Inner loop iteration 3 -> j = 2
Print Star

And so many. This is how we can control our inner loop based on the outer loops control variable. Let’s see the program now.

Code:

<?php
function print_pattern($num)
{
// Outer loop handles number of rows
for ($i = 0; $i < $num; $i++)
{
// inner loop handles number of columns
for($j = 0; $j <= $i; $j++ )
{
// Print stars
echo "* ";
}
// go to new line after each row pattern is printed
echo "\n";
}
}
//Call function and send number of lines as parameter
$num = 5;
print_pattern($num);
?>