i want to display numbers only from 01 to 12

How to write a loop

<?php  
for($a=0; $a<10; $a++)  
 {   
   for($b=0; $b<10; $b++)  
      {  
         echo $a.$b.", ";   
      }  
 }   
?>  

Dani AI

Generated

The simplest, clearest approach is to iterate 1 through 12 and format each value as a two‑digit string when outputting. ’s nested loops show why a 00–99 grid is overkill for this need, and was correct to recommend padding single‑digit values. Below are compact alternatives that keep logic simple and avoid generating unwanted values.

foreach (range(1, 12) as $n) {
    echo sprintf('%02d', $n) . PHP_EOL;
}

For an HTML month select where the option value stays numeric but the label is zero‑padded:

echo "<select name=\"month\">\n";
foreach (range(1, 12) as $m) {
    printf("  <option value=\"%d\">%02d</option>\n", $m, $m);
}
echo "</select>\n";

Notes and troubleshooting:

  • Store months as integers for calculations and format (with sprintf/printf or similar) only when displaying.
  • Avoid using numeric literals with leading zeros in source code; treat leading zeros as presentation, not data.
  • For just 12 items performance is irrelevant—readability matters more, so prefer range() or a single loop over nested loops and filtering.

Recommended Answers

All 2 Replies

I do not know whether is in your server php online

learn to use php sandbox

without the php tag

for($a=0; $a<10; $a++)
{
for($b=0; $b<10; $b++)
{
echo $a.$b."<br>\n";
}
} 

your grind count on 0 to 99

00<br>
01<br>
02<br>
03<br>
04<br>
05<br>
06<br>
07<br>
08<br>
09<br>
10<br>
11<br>
12<br>
13<br>
14<br>
15<br>
16<br>
17<br>
18<br>
19<br>
20<br>
21<br>
22<br>
23<br>
24<br>
25<br>
26<br>
27<br>
28<br>
29<br>
30<br>
31<br>
32<br>
33<br>
34<br>
35<br>
36<br>
37<br>
38<br>
39<br>
40<br>
41<br>
42<br>
43<br>
44<br>
45<br>
46<br>
47<br>
48<br>
49<br>
50<br>
51<br>
52<br>
53<br>
54<br>
55<br>
56<br>
57<br>
58<br>
59<br>
60<br>
61<br>
62<br>
63<br>
64<br>
65<br>
66<br>
67<br>
68<br>
69<br>
70<br>
71<br>
72<br>
73<br>
74<br>
75<br>
76<br>
77<br>
78<br>
79<br>
80<br>
81<br>
82<br>
83<br>
84<br>
85<br>
86<br>
87<br>
88<br>
89<br>
90<br>
91<br>
92<br>
93<br>
94<br>
95<br>
96<br>
97<br>
98<br>
99<br>
for($i=1; $i<=12; $i++){
    echo str_pad($i, 2, "0", STR_PAD_LEFT)."<br/>";
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.