Number Pattern in PHP: Top 5 Every Developer Must Learn
Number pattern programs in PHP are a very common exercise in programming. It helps developers improve their logical thinking and problem-solving skills. In addition, if you are going for an interview, any of these are likely to be one of the questions your interviewer is going to ask. Therefore, in this article, we will explore the most important number pattern programs in PHP, with detailed explanations for each pattern program.
Firstly, I already have an article on Triangular star pattern programs with examples. However, if you are new to PHP pattern programs, I suggest you have a look at star patterns in PHP before learning number patterns in PHP.
#1:Simple triangular Number Pattern in PHP
This number pattern program is the initial step in learning pattern programs in PHP. Moreover, in this number pattern, I am writing a PHP program to create five lines of triangular number pattern in increasing order.
$patternRows = 5;
for ($i = 1; $i <= $patternRows; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo $j . ' ';
}
echo "\n";
}
Code Explanation:
This triangle number pattern PHP code generates a pattern of numbers in rows using nested loops. Let’s break down the code line by line:
$patternRows = 5;
In this line, a variable named $patternRows
is initialized and assigned the value 5
. This variable determines the number of rows in the pattern.
for ($i = 1; $i <= $patternRows; $i++) {
Here begins the outer loop, which is responsible for iterating through each row of the pattern. Thus, It initializes a variable $i
with a value of 1
, and the loop continues as long as $i
is less than or equal to the value of $patternRows
. In each iteration, $i
is incremented by 1
.
for ($j = 1; $j <= $i; $j++) {
Inside the outer loop, an inner loop is started using a variable $j
. This inner loop generates the numbers for each row. It initializes $j
with a value of 1
and continues as long as $j
is less than or equal to the value of $i
. Each iteration of the inner loop $j
is incremented by 1
.
echo $j . ' ';
Inside the inner loop, the current value of $j
(which corresponds to the number being printed) is echoed followed by a space. This prints the numbers in increasing order within each row.
}
echo "\n";
After the inner loop finishes executing, subsequently, a newline character (\n) is echoed. As a result, this action moves the output to the next line, thereby creating a new row for the pattern in the output.
}
The closing brace ends the inner loop, and the outer loop continues to the next iteration if the condition $i <= $patternRows
is still met.
After the code executes, it will produce the following pattern in the output:
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Each row corresponds to the iteration of the outer loop. Likewise, the numbers in each row correspond to the iteration of the inner loop within that row.
#2. Right angled Triangle number Pattern in PHP
This pattern is for a right-angled Triangle number Pattern in PHP. If you see the previous pattern, it is a flip version of that pattern. Let’s see the code and then its explanation.
$patternRows = 5;
for ($i = 1; $i <= $patternRows; $i++) {
echo str_repeat(' ', $patternRows - $i); // Print leading 2 spaces
for ($j = 1; $j <= $i; $j++) {
echo $j . ' ';
}
echo "\n";
}
Line-wise code explanation
This PHP code generates a pattern with numbers in a triangular form but with each number followed by double spaces for indentation. Let’s break down the code line by line:
$patternRows = 5;
Here, the variable $patternRows
is initialized and set to 5
. This variable represents the number of rows in the pattern.
for ($i = 1; $i <= $patternRows; $i++) {
This line starts a for
loop that initializes $i
with the value 1
and increments $i
in each iteration. The loop continues as long as $i
is less than or equal to the value of $patternRows
. This loop, in turn, controls the rows of the pattern.
echo str_repeat(' ', $patternRows - $i); // Print leading 2 spaces
The main Loop to repeat the spaces
Within the loop, this line uses the str_repeat
function to print a specific number of leading spaces before the numbers in each row. The argument ' '
specifies a sequence of characters (spaces) that will be repeated a certain number of times. The value $patternRows - $i
calculates the number of repetitions, effectively creating an indentation effect that decreases with each row.
for ($j = 1; $j <= $i; $j++) {
Inside the outer loop, this line initializes an inner for
loop that iterates from $j = 1
to $j
being less than or equal to the current value of $i
. This loop controls the numbers within each row of the pattern.
echo $j . ' ';
Inside the inner loop, this line echoes the current value of $j
, followed by two spaces (' '
). This prints the numbers in increasing order for the current row, and the double spaces create the desired spacing between the numbers.
echo "\n";
After the inner loop completes, subsequently, this line echoes a newline character (\n) to move to the next line. As a result, this creates the effect of moving to the next row of the pattern.
}
}
The inner loop’s closing curly brace indicates the end of the inner loop. The outer loop’s closing curly brace indicates the end of the outer loop.
Output:
When you run this PHP code, it will produce the following output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Putting it all together, the code generates a right-angled triangle number pattern that has a right-side alignment. The str_repeat
function is used to print the appropriate number of leading spaces before each row of numbers. The inner loop manages the quantity of numbers to print in each row. The outer loop manages the overall structure of the triangle with the specified number of rows.
If the double spaces are not visible in your case, as it happens sometimes, try to add blank space characters like ‘ ’
#3. Pyramid Number Pattern:
$patternRows = 5;
for ($i = 1; $i <= $patternRows; $i++) {
echo str_repeat(' ', $patternRows - $i); // Print spaces
for ($j = 1; $j <= $i; $j++) {
echo $j . ' ';
}
echo "\n";
}
This PHP Pyramid Number Pattern code generates a pattern of numbers in a triangular(Pyramid) form with each row containing increasing numbers. Let’s break down the code line by line:
Code Explanation:
$patternRows = 5;
In this line, a variable named $patternRows
is initialized and assigned the value 5
. This variable represents the number of rows in the pattern triangle.
for ($i = 1; $i <= $patternRows; $i++) {
This line initiates a for
loop that iterates from $i = 1
to $i
being less than or equal to the value stored in $patternRows
(which is 5 in this case). This loop controls the rows of the pattern.
echo str_repeat(' ', $patternRows - $i); // Print spaces
Inside the loop, this line uses the str_repeat
function to print a specific number of spaces before the numbers in each row. The number of spaces printed decreases as the loop progresses. The argument ' '
specifies the space character, and the value $patternRows - $i
calculates the number of spaces needed.
for ($j = 1; $j <= $i; $j++) {
Within the outer loop, this line initiates another for
loop that iterates from $j = 1
to $j
being less than or equal to the current value of $i
. This loop controls the numbers within each row of the pattern.
echo $j . ' ';
Inside the inner loop, this line echoes the current value of $j
followed by a space character. This prints the numbers in increasing order for the current row.
echo "\n";
After the inner loop completes, this line echoes a newline character (\n
) to move to the next line. This creates the effect of moving to the next row of the pattern.
}
The inner loop’s closing curly brace indicates the end of the inner loop.
}
The outer loop’s closing curly brace indicates the end of the outer loop.
When you run this PHP code, it will produce the following output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
The code uses nested loops to control the spacing and the numbers printed in each row, creating a triangular pattern of increasing numbers.
#4. Diamond Number Pattern:
$patternRows = 5;
for ($i = 1; $i <= $patternRows; $i++) {
echo str_repeat(' ', $patternRows - $i); // Print spaces
for ($j = 1; $j <= $i; $j++) {
echo $j . ' ';
}
echo "\n";
}
for ($i = $patternRows - 1; $i >= 1; $i--) {
echo str_repeat(' ', $patternRows - $i); // Print spaces
for ($j = 1; $j <= $i; $j++) {
echo $j . ' ';
}
echo "\n";
}
This PHP code generates a pattern that resembles a diamond /rhombus shape with rows of increasing and decreasing numbers. It consists of two parts: the first part creates the top half which is increasing like a triangle, and the second part creates the bottom half.
Code Explanation:
Let’s break down the code line by line for each part:
Part 1: Creating the Top Half of the Diamond Pattern program in PHP
$patternRows = 5;
for ($i = 1; $i <= $patternRows; $i++) {
echo str_repeat(' ', $patternRows - $i); // Print spaces
for ($j = 1; $j <= $i; $j++) {
echo $j . ' ';
}
echo "\n";
}
This part of the code generates a diamond/rhombus pattern starting from the top. The outer loop ($i
loop) runs from 1
to $patternRows
, incrementing the value of $i
with each iteration. This controls the number of rows in the pattern.
The first inner loop ($j
loop) within the $i
loop is responsible for printing the numbers in increasing order for each row.
- The
str_repeat
function prints a specific number of spaces before the numbers. The number of spaces printed decreases as the loop progresses. - The second inner loop (
$j
loop) iterates from1
to the current value of$i
, printing the numbers in increasing order for the current row. - The newline character (
\n
) is echoed at the end of each row to move to the next line.
Part 2: Creating the Bottom Half of the diamond/rhombus
for ($i = $patternRows - 1; $i >= 1; $i--) {
echo str_repeat(' ', $patternRows - $i); // Print spaces
for ($j = 1; $j <= $i; $j++) {
echo $j . ' ';
}
echo "\n";
}
In this part of the code, I have created the bottom half of the diamond/rhombus number pattern. The outer loop ($i
loop) runs from $patternRows - 1
down to 1
, decrementing the value of $i
with each iteration.
The structure of this part is similar to the first part, with the difference that the pattern is in reverse order. The decreasing values of $i
create the decreasing number of rows.
Overall, when you run this code, it will produce the following diamond/rhombus number pattern in PHP:
The code uses nested loops to control the spacing and the numbers printed in each row, creating the diamond/rhombus number pattern in PHP with both the top and bottom halves.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
#5. Inverted Triangle/ Inverted Pyramid Number Pattern in PHP:
The inverted triangle/ Inverted Pyramid Number Pattern in PHP flips the pyramid upside down.
$patternRows = 5;
for ($i = $patternRows; $i >= 1; $i--) {
echo str_repeat(' ', $patternRows - $i); // Print spaces
for ($j = 1; $j <= $i; $j++) {
echo $j . ' ';
}
echo "\n";
}
This PHP code generates a pattern that resembles an upside-down pyramid with rows of increasing numbers.
Code Explanation:
Let’s break down the code line by line:
$patternRows = 5;
Here, the variable $patternRows
is initialized and set to 5
. This variable represents the number of rows in the pattern.
for ($i = $patternRows; $i >= 1; $i--) {
This line starts a for
loop that initializes $i
with the value of $patternRows
and decrements $i
in each iteration. The loop continues as long as $i
is greater than or equal to 1
. This loop controls the rows of the pattern, creating a decreasing pattern.
echo str_repeat(' ', $patternRows - $i); // Print spaces
Within the loop, this line uses the str_repeat
function to print a specific number of spaces before the numbers in each row. The number of spaces printed decreases as the loop progresses. The argument ' '
specifies the space character, and the value $patternRows - $i
calculates the number of spaces needed.
for ($j = 1; $j <= $i; $j++) {
Inside the outer loop, this line initializes an inner for
loop that iterates from $j = 1
to $j
being less than or equal to the current value of $i
. This loop controls the numbers within each row of the pattern.
echo $j . ' ';
Inside the inner loop, this line echoes the current value of $j
followed by a space character. This prints the numbers in increasing order for the current row.
echo "\n";
After the inner loop completes, this line echoes a newline character (\n
) to move to the next line. This creates the effect of moving to the next row of the pattern.
}
The inner loop’s closing curly brace indicates the end of the inner loop.
}
The outer loop’s closing curly brace indicates the end of the outer loop.
When you run this PHP code, it will produce the following output:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
The code uses nested loops to control the spacing and the numbers printed in each row, creating an upside-down pyramid pattern with decreasing numbers.