Write a program to count 5 to 15 using PHP loop
Description: Write a PHP program to display the numbers from 5 to 15, one number in each line, using a PHP loop.
Rules and hints:
- Use a
foror awhileloop. - Use a variable for the counter.
- Use the HTML tag
<br>for the line break.
PHP program to count 5 to 15 using a while loop
<?php
$count = 5;
while ($count <= 15) {
echo $count . "<br>";
$count++;
}
?>
Output:
Tutorials Class - Output Window
5
6
7
8
9
10
11
12
13
14
15
The same with a for loop
<?php
for ($count = 5; $count <= 15; $count++) {
echo $count . "<br>";
}
?>
How it works
- The counter starts at 5. The loop runs while the counter is 15 or less.
$count++adds 1 in every round, so the loop ends after 15.
Learn more about the similar topics:
| Tutorials |
|---|
| PHP Loops |
| Interview Questions & Answers |
|---|
| How to terminate or break a loop in PHP? |