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 for or a while loop.
  • 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
Exercises & Assignments
Write a program to count 5 to 15 using PHP loop
Write a factorial program using for loop in php
Write a program to create Chess board in PHP using for loop
Write a Program to create given pattern with * using for loop
Write a PHP script to displays numbers between 100 and 150 divisible by 4
Interview Questions & Answers
How to terminate or break a loop in PHP?