Now you have the ability to repeat execution of code blocks as necessary in your PHP programs.
Here is a summary of the topics covered in this lesson:
whileloops execute only as long as their conditional evaluates to
whileloops always execute at least once and then continue executing while their conditional is
forloops contain 3 expressions and are frequently used to execute a code block a specific number of times.
- The first expression is executed prior to the first iteration.
- The second expression is evaluated prior to each iteration. If
TRUE, the code block executes. Otherwise, the loop terminates.
- The third expression is evaluated after each iteration.
foreachloops are used for iterating over the elements of an array. The key and value of each element is available in the code block.
breakis used to end execution of a loop early.
continueis used to end execution of a loop iteration early and continues to the next iteration.
The workspace contains some examples of loops in PHP. You can use this as a sandbox to make sure you understand these concepts.