Learn
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:
while
loops execute only as long as their conditional evaluates toTRUE
.do
…while
loops always execute at least once and then continue executing while their conditional isTRUE
.for
loops 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.
foreach
loops are used for iterating over the elements of an array. The key and value of each element is available in the code block.break
is used to end execution of a loop early.continue
is used to end execution of a loop iteration early and continues to the next iteration.
Instructions
The workspace contains some examples of loops in PHP. You can use this as a sandbox to make sure you understand these concepts.
Take this course for free
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.