# PHP Loops

Here, you will find Assignments & Exercises related to PHP Loops. Once you learn PHP Loops, it is important to practice on these to understand concepts well.

These PHP Loop exercises contain programs on PHP for Loop, while Loop, mathematical series, and various string pattern designs.

All PHP exercises are available in the form of PHP problem, description and solution. These PHP programs can also be used as assignments for PHP students.

## Write a program to count 5 to 15 using PHP loop

Description:
Write a Program to display count, from 5 to 15 using PHP loop as given below.

Rules & Hint

• You can use “for” or “while” loop
• You can use variable to initialize count
• You can use html tag for line break

View Solution/Program

``````<?php
\$count = 5;
while(\$count <= 15)
{
echo \$count;
echo "<br>" ;

\$count++;
}
?>``````
Tutorials Class - Output Window

5
6
7
8
9
10
11
12
13
14
15

## Write a factorial program using for loop in php

Description:

Write a program to calculate factorial of a number using for loop in php.

``````<?php
\$num = 3;
\$factorial = 1;

for (\$x=\$num; \$x>=1; \$x--)
{
\$factorial = \$factorial * \$x;
}

echo "The factorial of \$num is \$factorial";
?>``````
Tutorials Class - Output Window

The factorial of 3 is 6

## Write a program to create Chess board in PHP using for loop

Description:

Write a PHP program using nested for loop that creates a chess board.

Conditions:

• You can use html table having width=”400px” and take “30px” as cell height and width for check boxes.

View Solution/Program

``````<table width="400px" cellspacing="0px" cellpadding="0px" border="1px">
<?php
for(\$row=1;\$row<=8;\$row++)
{
echo "<tr>";
for(\$column=1;\$column<=8;\$column++)
{
\$total=\$row+\$column;
if(\$total%2==0)
{
echo "<td height=35px width=30px bgcolor=#FFFFFF></td>";
}
else
{
echo "<td height=35px width=30px bgcolor=#000000></td>";
}
}
echo "</tr>";
}
?>
</table>``````
Tutorials Class - Output Window

## Write a Program to create given pattern with * using for loop

Description:
Write a Program to create following pattern using for loops:

```*
**
***
****
*****
******
*******
********
```

Rules

• You can use for or while loop
• You can use multiple (nested) loop to draw above pattern

View Solution/Program using two for loops

``````<?php
for(\$row=1;\$row<=8;\$row++)
{
for (\$star=1;\$star<=\$row;\$star++)
{
echo "*";
}
echo "<br>";
}
?>``````
Tutorials Class - Output Window

*
**
***
****
*****
******
*******
********