Repeated Number Triangle in PHP

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a repeated number triangle in PHP using nested for loops. Row $i prints the digit $i exactly $i times.

This is a simple way to see how the outer loop controls the digit and the inner loop controls the repetition count.

⭐ Pattern Output

For $n = 5, the pattern looks like this:

Output
1
22
333
4444
55555
1

Complete PHP Program

Fixed $n = 5 version:

PHP
<?php
$n = 5;

for ($i = 1; $i <= $n; $i++) {
    for ($j = 1; $j <= $i; $j++) {
        echo $i;
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Choose the size

$n = 5; sets the number of rows.

Setup
2

Outer loop (digit selection)

for ($i = 1; $i <= $n; $i++) selects which digit to repeat on the row.

Row control
3

Inner loop (repeat $i times)

for ($j = 1; $j <= $i; $j++) repeats echo $i; exactly $i times.

Number printing
4

New line

echo PHP_EOL; ends the row and moves to the next line.

Line break
=

Repeated number triangle

The total printed digits are 1+2+…+n = n(n+1)/2, so time complexity is O(n²).

2

Variation — User Input (CLI) Version

Let the user decide $n at runtime (run from terminal with php):

PHP
<?php
echo "Enter the value of n: ";
$n = (int) trim(fgets(STDIN));

for ($i = 1; $i <= $n; $i++) {
    for ($j = 1; $j <= $i; $j++) {
        echo $i;
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Validate input (reject $n < 1) before printing
  • Add spaces between numbers (for example, echo $i . " ";)
  • Print the descending-row version by counting the outer loop down
  • Use a running counter instead of $i to create Floyd’s triangle
  • Try printing stars instead of numbers for a star triangle

Avoid

  • Forgetting the newline after each row
  • Using negative or zero values without validating input
  • Printing the wrong variable (this pattern prints $i, not $j)
  • Assuming CLI input is always valid (guard fgets() and casting if needed)

Key Takeaways

1

The outer loop selects which number to print on the row.

2

The inner loop controls how many times the number repeats.

3

Total printed digits follow n(n+1)/2, so runtime is O(n²).

4

This pattern helps you separate the “value” and the “count” when thinking about loops.

❓ Frequently Asked Questions

Because when $i = 5, the inner loop runs 5 times and prints $i each time.
Set $n to the number of rows you want. The last row will then repeat $n exactly $n times.
Yes—replace echo $i; with echo 1; so every row repeats the same digit.
O(n²) for n rows: total prints are 1+2+…+n = n(n+1)/2.

Next: PHP Number Pattern 10

Continue to Program 10 for the next number pattern exercise in PHP.

Program 10 →
Did you know?

Repeated-digit patterns are a great way to see how one variable can represent the value while another variable represents the repetition count.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful