PHP Basics Test Chamber

Loops and Repetition

Loops are used to repeat a section of code, based on certain criteria. They are important because copying and pasting or rewriting code many times can introduce problems and bugs and is very inefficient.

While Loop

The most generic kind of loop; it will continue repeating as long as the condition evaluates to true. A condition that always evaluates to true will create a infinite loop that may crash your web server.

0

1

2

3

4

5

6

7

8

9

Do ... While Loop

Basically the same thing as a 'while' loop, but your code will execute at least once, regardless of the condition. This is really the only situation where a do ... while loop is appropriate.

0

1

2

3

4

5

6

7

8

9

For Loop

A loop that has a built-in counter, useful when you need to look a specific number of times. You must know how many times you will repeat the code before the loop begins.

1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79,81,83,85,87,89,91,93,95,97,99,

Foreach Loop

A loop that repeats as many times as there are elements in an array. This is a PHP-specific loop.