PHP Loops
PHP loop exercises: numbers divisible by 4, a star pattern, a chess board, factorial with a for loop and counting from 5 to 15.
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:
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.
Write a factorial program using for loop in php
Description: Write a PHP program to calculate the factorial of a number using a for loop.
Rules and hints:
- Factorial of 5 is 5 x 4 x 3 x 2 x 1 = 120.
- Use a loop that multiplies the numbers one by one.
PHP program for factorial using a for loop
<?php
$num = 5;
$factorial = 1;
for ($x = $num; $x >= 1; $x--) {
$factorial = $factorial * $x;
}
echo "The factorial of " . $num . " is " . $factorial;
?>
Output:
The factorial of 5 is 120
How it works
- $factorial starts with 1.
- The loop goes from 5 down to 1, and multiplies $factorial with each number.
- The factorial of 0 is 1, and this program gives 1 for it too, because the loop does not run.
Another way is to use a function that calls itself: factorial using recursion.
Write a program to create Chess board in PHP using for loop
Description: Write a PHP program that creates a chess board using nested for loops.
Rules and hints:
- Use an HTML table. Make each square 50px wide and 50px high.
- The squares must be black and white, one after the other.
- Hint: if the sum of the row number and the column number is even, use white, otherwise black.
PHP program to create a chess board
<?php
echo '<table border="1" cellspacing="0" cellpadding="0">';
for ($row = 1; $row <= 8; $row++) {
echo "<tr>";
for ($col = 1; $col <= 8; $col++) {
$color = (($row + $col) % 2 == 0) ? "white" : "black";
echo '<td style="width:50px;height:50px;background:' . $color . '"></td>';
}
echo "</tr>";
}
echo "</table>";
?>
Output:
An 8 x 8 table of black and white squares, like a chess board
How it works
- The outer loop creates the 8 rows (
<tr>), and the inner loop creates 8 cells (<td>) in every row. ($row + $col) % 2is 0 for every second square, so the colors change like on a real chess board.- The ternary operator picks the color. See PHP Ternary Operator.
Write a Program to create given pattern with * using for loop
Description: Write a PHP program to print this pattern using loops:
*
**
***
****
*****
******
*******
********
Rules and hints:
- You can use a
foror awhileloop. - Use a nested loop (a loop inside a loop).
PHP program to print the star pattern using two for loops
<?php
for ($row = 1; $row <= 8; $row++) {
for ($star = 1; $star <= $row; $star++) {
echo "*";
}
echo "<br>";
}
?>
Output:
*
**
***
****
*****
******
*******
********
How it works
- The outer loop makes 8 rows.
- The inner loop prints as many stars as the number of the current row (1 star in row 1, 2 stars in row 2, and so on).
- After every row,
<br>moves to the next line.
Shorter version
<?php
for ($row = 1; $row <= 8; $row++) {
echo str_repeat("*", $row) . "<br>";
}
?>
Write a PHP script to displays numbers between 100 and 150 divisible by 4
Description: Write a PHP script to display the numbers between 100 and 150 that are divisible by 4.
Rules and hints:
- Use a loop and an
ifcondition. - Use the modulus operator (
%). A number is divisible by 4 when$i % 4is 0.
PHP program for numbers divisible by 4
<?php
for ($i = 100; $i <= 150; $i++) {
if ($i % 4 == 0) {
echo $i . " ";
}
}
?>
Output:
100 104 108 112 116 120 124 128 132 136 140 144 148
How it works
- The loop checks every number from 100 to 150.
%gives the remainder of the division. If the remainder is 0, the number is divisible by 4, and it is printed.
A shorter way
Since 100 is divisible by 4, you can start from 100 and add 4 in every round. You do not need the if condition.
<?php
for ($i = 100; $i <= 150; $i += 4) {
echo $i . " ";
}
?>
Related: PHP Loops and division vs modulus.