0
0
PhpProgramBeginner · 2 min read

PHP Program to Print Numbers 1 to n

Use a for loop in PHP like for ($i = 1; $i <= $n; $i++) { echo $i . "\n"; } to print numbers from 1 to n.
📋

Examples

Input1
Output1
Input5
Output1 2 3 4 5
Input0
Output
🧠

How to Think About It

To print numbers from 1 to n, start counting from 1 and keep increasing by 1 until you reach n. For each number, print it on a new line. If n is zero or negative, print nothing.
📐

Algorithm

1
Get the input number n.
2
Start a counter i at 1.
3
While i is less than or equal to n, do the following:
4
Print the current value of i.
5
Increase i by 1.
6
Stop when i becomes greater than n.
💻

Code

php
<?php
$n = 5; // You can change this value
for ($i = 1; $i <= $n; $i++) {
    echo $i . "\n";
}
?>
Output
1 2 3 4 5
🔍

Dry Run

Let's trace the program with n = 3 through the code

1

Initialize n and i

n = 3, i = 1

2

Check condition i <= n

1 <= 3 is true, so enter loop

3

Print i

Print 1

4

Increment i

i becomes 2

5

Repeat condition check

2 <= 3 is true, print 2, increment i to 3

6

Repeat condition check

3 <= 3 is true, print 3, increment i to 4

7

Condition fails

4 <= 3 is false, exit loop

iCondition i <= nAction
1truePrint 1, i++ to 2
2truePrint 2, i++ to 3
3truePrint 3, i++ to 4
4falseExit loop
💡

Why This Works

Step 1: Initialize counter

We start counting from 1 using $i = 1 because we want to print from 1 to n.

Step 2: Loop condition

The loop runs while $i <= $n to ensure we print all numbers up to n.

Step 3: Print and increment

Inside the loop, we print the current number and then increase $i by 1 to move to the next number.

🔄

Alternative Approaches

while loop
php
<?php
$n = 5;
$i = 1;
while ($i <= $n) {
    echo $i . "\n";
    $i++;
}
?>
Uses a while loop instead of for; functionally the same but some find for loops cleaner for counting.
do-while loop
php
<?php
$n = 5;
$i = 1;
do {
    echo $i . "\n";
    $i++;
} while ($i <= $n);
?>
Ensures the loop runs at least once; useful if you want to print at least one number even if n is less than 1.
recursive function
php
<?php
function printNumbers($i, $n) {
    if ($i > $n) return;
    echo $i . "\n";
    printNumbers($i + 1, $n);
}
printNumbers(1, 5);
?>
Uses recursion to print numbers; elegant but less efficient and can cause stack overflow for very large n.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs exactly n times, so the time grows linearly with n, making it O(n).

Space Complexity

The program uses a fixed amount of memory regardless of n, so space complexity is O(1).

Which Approach is Fastest?

The for loop and while loop have similar performance; recursion is slower and uses more memory.

ApproachTimeSpaceBest For
for loopO(n)O(1)Simple and clear counting
while loopO(n)O(1)Similar to for, flexible condition
do-while loopO(n)O(1)At least one iteration guaranteed
recursive functionO(n)O(n)Elegant but uses more memory
💡
Use a for loop for simple counting tasks like printing numbers from 1 to n.
⚠️
Starting the loop counter at 0 instead of 1 when you want to print from 1 to n.