Home >Backend Development >PHP Tutorial >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.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
So, keeping all of this in mind, let’s try to code the patterns now.
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 –> <em>j </em>=<em> 0</em> Print star Outer loop iteration 2 –> i<em> = 1</em> Inner loop iteration 1 –> <em>j</em><em> =</em><em> 0</em> Print Star Inner loop iteration 2 -> <em>j</em> =<em> 1 </em>Print Star Outer loop iteration 3 –> <em>i</em> =<em> 2 </em>Inner loop iteration 1 –> <em>j =</em> 0 Print Star Inner loop iteration 2 -> <em>j = 1 </em>Print Star Inner loop iteration 3 -> <em>j = 2 </em>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:
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.
Note: Keep one thing in mind that the number of spaces in the k-loop is double space. This is because we print a single-space along with the stars as well. This gives a finished look to our pattern rather than a congested print. We will use this to our leverage when we print full pyramids.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:
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:
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:
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:
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:
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:
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.
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.
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.
The above is the detailed content of Patterns in PHP. For more information, please follow other related articles on the PHP Chinese website!