Learn
Loops
while

The first PHP loop that we will cover is the while loop. This type of loop continues to iterate as long as its conditional is true.

This code outputs the numbers from 1-10, similar to the previous example:

\$count = 1; while (\$count < 11) { echo "The count is: " . \$count . "\n"; \$count += 1; }

The first time the interpreter encounters this code, it checks the condition. If it evaluates to TRUE, the code block is executed. It then checks the condition again, and if TRUE, executes the code block again. It continues in this fashion until the condition is FALSE.

In this example, the code within the curly braces ({}) executes while the conditional statement within the brackets (\$count < 11) is still true. \$count starts at a value of 1 so the conditional is true on the first iteration.

The variable \$count is incremented by 1 during each iteration of the loop (\$count += 1). When \$count is equal to 11, the conditional is no longer true and the while loop terminates. Any code after this block is then executed.

### Instructions

1.

We want to examine every number from 1 to 100 and print the number followed by " is divisible by 33\n" if the number is divisible by 33.

Begin by setting up a loop that counts from 1 to 100.

There are many ways to do this, but for this exercise:

• Use a \$count variable initially set to 1.
• Use a while loop with a conditional that becomes false when \$count is greater than 100.
• Increment \$count by 1 at the end of each loop iteration.
2.

At the beginning of the code block within the while loop, add an if statement that only executes if \$count is divisible by 33. Use the modulo operator.

The if code block should print the \$count followed by " is divisible by 33\n".