Updated March 16, 2023
Introduction to Patterns in PHP
What is Pattern Programming in PHP? It is an art of programming to print some sort of pattern on the screen. This can be a series of numbers, letters, or special characters to form a pattern. The simplest example of a pattern is the Fibonacci series (1, 1, 2, 3, 5, 8, 13, 21, 34 and so on). Then there are other patterns which are designs on screen, say a pyramid of stars. So, basically, pattern programming is simply printing a pattern on the screen.
For this article, we would be using PHP to code the patterns. Don’t worry though. Once you get the hang of it, it’s just the syntax that varies from language to language. The logic is the same, always.
Examples of Pattern in PHP
- Before jumping right onto the programs, let us understand the basic logic of coding patterns. Patterns are always programmed through nested loops– mostly nested for loops. This is because loops are syntactically easier to understand and concise.
- The outer loop is always concerned with the number of lines. So, let’s 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’s 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 on the pattern, sometimes there are more than one inner loops or three levels of nested loops. Also, there is the involvement of spaces and tabs to produce the desired pattern.
So, keeping all of this in mind, let’s try to code the patterns now.
1. Star Half Pyramid
This is the simplest pattern to print. It prints the increasing number of stars in subsequent lines. 1 star in the first line, 2 stars in the second line and so on.
Let’s code this pattern for five lines. Keeping 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’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 –> j = 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 on. This is how we control our inner loop based on the outer loops control variable. Let us see the program in action 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);
?>
Output:
2. Star Half Pyramid– Mirrored
This is similar to the Star Half Pyramid, except that the stars are right-aligned.
To achieve the right indentation, we would use spaces and then print stars. So, there would be two inner loops– one to control the number of spaces and others to control the number of stars.
Code:
<?php
function print_pattern($num)
{
// Outer loop handles number of rows
for ($i = 0; $i < $num; $i++)
{
// inner loop handles indentation
for($k = $num; $k > $i+1; $k-- )
{
// Print stars
echo " ";
}
// inner loop handles number of stars
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);
?>
Output:
3. Star Half Pyramid– Inverted
For this pyramid pattern, the number of stars keeps decreasing with each new line. The first line has 5 stars, the second line has 4 stars and so on.
Keeping the logic in mind, we know that the outer loop always has to control the number of lines and the inner loop has to control the number of stars. This logic cannot be changed. Although, what can be changed is how we start the loops, increasing or decreasing order. This means we can either loop from 0 to 5 or we can loop in decreasing order from 5 to 0. So, for inverted patterns such as this, we know that the number of stars is more in the first line. So, we choose to decrease order loops.
Code:
<?php
function print_pattern($num)
{
// Outer loop handles number of rows
for ($i = $num; $i > 0; $i--){
// inner loop handles number of stars
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);
?>
Output:
4. Star Half Pyramid– Inverted Mirrored
This pattern is an indented inverted half pyramid. The number of stars decreases with each line and stars are right-aligned.
I believe by now you would be able to guess the logic for this one.
Code:
<?php
function print_pattern($num)
{
// Outer loop handles number of rows
for ($i = $num; $i > 0; $i--)
{
// inner loop handles indentation
for($k = $i; $k < $num; $k++ )
{
// Print stars
echo " ";
}
// inner loop handles number of stars
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);
?>
Output:
5. Star Full Pyramid
This pattern prints the full pyramid. Or in other words, it prints a triangle of stars on the screen.
This pattern is essentially a combination of Half pyramid and its mirror. Although there is a slight twist when we code it. Revisit the Note in Mirrored Half Pyramid. Remember, how we used double spacing to give a finished look to our pattern? Here we would use single spacing so that the stars are alternately aligned in odd and even number of rows, giving us a true triangular pattern.
Code:
<?php
function print_pattern($num)
{
// Outer loop handles number of rows
for ($i = 0; $i < $num; $i++)
{
// inner loop handles indentation
for($k = $num; $k > $i+1; $k-- )
{
// Print spaces
echo " ";
}
// inner loop handles number of stars
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);
?>
Output:
6. Star Diamond
This pattern prints a complete diamond shape on the screen. The number of stars increases until the maximum defined and then decrease back to 1, giving us a full diamond shape.
To print this pattern, we would need to divide the pattern into two halves. The upper half – which prints the increasing number of stars. The lower half– which prints the decreasing number of stars. To print both the halves, we would use two outer loops and corresponding inner loops.
Code:
<?php
function print_pattern($num)
{
// The Upper Half Pattern
// Outer loop handles number of rows
for ($i = 0; $i < $num; $i++)
{
// inner loop handles indentation
for($k = $num; $k > $i+1; $k-- )
{
// Print spaces
echo " ";
}
// inner loop handles number of stars
for($j = 0; $j <= $i; $j++ )
{
// Print stars
echo "* ";
}
// go to new line after each row pattern is printed
echo "\n";
}
// The Lower Half Pattern
// Outer loop handles number of rows
for ($i = $num-1; $i > 0; $i--)
{
// inner loop handles indentation
for($k = $num-1; $k >= $i; $k-- )
{
// Print spaces
echo " ";
}
// inner loop handles number of stars
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);
?>
Output:
7. Number Pattern
For this number pattern, we will print the numbers in relation to the row number. Thus, digit 1 would be printed once, 2 twice, 3 thrice and so on.
If you would have followed this tutorial line by line, by now you must have understood very well the working of nested loops to print patterns. This pattern also follows the same logic. Instead of stars, we print numbers. Now you ask how do we get the numbers? The answer is- simply through our control variables i and j.
Code:
<?php
function print_pattern($num)
{
// Outer loop handles number of rows
for ($i = 1; $i <= $num; $i++)
{
// inner loop handles indentation
for($k = $num; $k > $i; $k-- )
{
// Print spaces
echo " ";
}
// inner loop handles number of stars
for($j = 1; $j <= $i; $j++ )
{
// Print numbers
echo $i." ";
}
// 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);
?>
Output:
8. Character Pattern
In this pattern, we would print the alphabets ABCDE in a pattern. Starting with A, the subsequent rows would introduce a new alphabet sandwiched between the previous alphabets.
The only trick in this pattern is to get the characters from our control variables. We do this by leveraging the ASCII value of the characters. The ASCII value of A to Z is 65 to 90. So, we calculate the ASCII value in each iteration and print the corresponding character. The chr() function in PHP is used to print a character from the ASCII code.
Code:
<?php
function print_pattern($num)
{
// Outer loop handles number of rows
for ($i = 1; $i <= $num; $i++)
{
// inner loop handles indentation
for($k = $num; $k > $i; $k-- )
{
// Print spaces
echo " ";
}
// inner loop handles number of stars
for($j = 1; $j <= $i; $j++ )
{
// Print characters
echo chr(64+$j)." ";
}
for($j = $i-1; $j >= 1; $j-- )
{
// Print characters
echo chr(64+$j)." ";
}
// 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);
?>
Output:
Print for full alphabets and the pattern looks pretty cool.
9. The Binary Hourglass– Bonus Pattern
This pattern is a dynamic pattern that prints the hourglass relative to the time elapsed, not an actual calculation though. For e.g., if one hour has elapsed, it will print one line of 0s in the upper half and one line of 1s in the lower half.
Code:
<?php
function print_pattern($num, $hour)
{
// Outer loop handles number of rows
for ($i = $num; $i > 0; $i--)
{
// inner loop handles indentation
for($k = $num; $k > $i; $k-- )
{
// Print spaces
echo " ";
}
// inner loop handles number of stars
for($j = 0; $j < $i; $j++ )
{
// Print characters
if($num-$i < $hour)
echo "0 ";
else
echo "1 ";
}
// go to new line after each row pattern is printed
echo "\n";
}
for ($i = 1; $i < $num; $i++)
{
// inner loop handles indentation
for($k = $num-1; $k > $i; $k-- )
{
// Print spaces
echo " ";
}
// inner loop handles number of stars
for($j = 0; $j <= $i; $j++ )
{
// Print characters
if($num-$i <= $hour)
echo "1 ";else
echo "0 ";
}
// go to new line after each row pattern is printed
echo "\n";
}
}
//Call function and send number of lines as parameter
$num = 8;
$hour = 3;
print_pattern($num, $hour);
?>
Output: 1 hour has elapsed.
Output: 2 hours have elapsed.
Output: 3 hours have elapsed.
And so on.
Conclusion
There is a lot to play with patterns. It’s all about keeping the code logic in mind. Once you get the code logic completely understood, there is no pattern you can’t print.
Recommended Articles
This has been a guide to Patterns in PHP. Here we have discussed Overviews and Examples of Pattern in PHP with codes and output. You can also go through our given articles to learn more-