Key Concepts

Review core concepts you need to learn to master this subject

PHP while loops

// This while loop counts from 0 to 100 $count = 0; while ($count <= 100) { echo "The count is: " . $count . "\n"; $count += 1; }

In PHP, while loops repeat execution of their code block as long as their conditional statement is true.

The syntax for a while loop is:

while (/*conditional*/) { #code block }

PHP do while loops

// This while loop counts from 0 to 100 $count = 0; while ($count <= 100) { echo "The count is: " . $count . "\n"; $count += 1; }

In PHP, do…while loops are very similar to while loops. The main difference is that do...while loops always execute their code block at least once and continue execution as long as their conditional statement is true. Even if the conditional is false, the code block will execute one time.

The syntax for a do…while loop is:

do { #code block } while (/*conditional*/);

PHP for loop

// This while loop counts from 0 to 100 $count = 0; while ($count <= 100) { echo "The count is: " . $count . "\n"; $count += 1; }

In PHP, a for loop is commonly used to execute a code block a specific number of times. The syntax makes use of three expressions:

for (#expression 1; #expression 2; #expression 3) { # code block }
  • The first is evaluated only one time before the first iteration
  • The second is evaluated before each iteration. If it is TRUE, the code block is executed. Otherwise, the loop terminates.
  • The third is evaluated after each iteration.

PHP foreach loop

// This while loop counts from 0 to 100 $count = 0; while ($count <= 100) { echo "The count is: " . $count . "\n"; $count += 1; }

In PHP, the foreach loop is used for iterating over an array. The code block is executed for every element in the array and the value of that element is available for use in the code block.

The syntax is:

foreach ($array as $value) { #code block }

On each iteration, a $value from $array is available for usage in the code block.

To access the keys as well as values in an array, the syntax can be modified to:

foreach ($array as $key => $value) { #code block }

PHP break keyword

// This while loop counts from 0 to 100 $count = 0; while ($count <= 100) { echo "The count is: " . $count . "\n"; $count += 1; }

In PHP, break can be used to terminate execution of a for, foreach, while or do…while loop.

One downside of heavy usage of break statements is that code can become less readable.

PHP continue keyword

// This while loop counts from 0 to 100 $count = 0; while ($count <= 100) { echo "The count is: " . $count . "\n"; $count += 1; }

In PHP, continue can be used to terminate execution of a loop iteration during a for, foreach, while or do…while loop. The code execution continues with the next iteration of the loop.

The continue keyword is similar to break except it only ends the current iteration early, not the entire loop.

PHP for loop shorthand

// This while loop counts from 0 to 100 $count = 0; while ($count <= 100) { echo "The count is: " . $count . "\n"; $count += 1; }

In PHP, the shorthand for a for loop is:

for (/*condition*/): # code block endfor;

When embedding in HTML, this is preferable to the bracket syntax, since it is much more clear which code block is being ended with the endfor.

PHP foreach loop shorthand

// This while loop counts from 0 to 100 $count = 0; while ($count <= 100) { echo "The count is: " . $count . "\n"; $count += 1; }

In PHP, the shorthand for a foreach loop is:

foreach ($array as $value): # code block endforeach;

When embedding in HTML, this is preferable to the bracket syntax, since it is much more clear which code block is being ended with the endforeach.

PHP while loop shorthand

// This while loop counts from 0 to 100 $count = 0; while ($count <= 100) { echo "The count is: " . $count . "\n"; $count += 1; }

In PHP, the shorthand for a while loop is:

while(/*condition*/): # code block endwhile;

When embedding in HTML, this is preferable to the bracket syntax, since it is much more clear which code block is being ended with the endwhile.

  1. 1
    When attempting to repeat code over and over again, it can be monotonous to retype or copy and paste the same code. Worse, inadvertent typos can cause errors in your program. Consider an example w…
  2. 2
    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…
  3. 3
    A do…while loop is very similar to a while loop. The main difference is that the code block will execute once without the conditional being checked. After the first iteration, it behaves the same…
  4. 4
    A for loop is commonly used to execute a code block a specific number of times. for (#expression 1; #expression 2; #expression 3) { # code block } The for loop syntax includes 3 expressions: - …
  5. 5
    The foreach loop is used for iterating over an array. The code block is executed for every element in the array and the value of that element is available for use in the code block. Our counting t…
  6. 6
    Similar to switch statements, the break keyword can be used to terminate any of the loop types early. In our counting example of a while loop, if we add a conditional and a break statement: $count …
  7. 7
    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 c…
  1. 1
    Using the traditional loop syntax in PHP with brackets ({}) to open and close code blocks can be used when embedding PHP code in HTML: Duck Goose However, when adding nested loops, the…
  2. 2
    We’ve already covered the shorthand for for loops in PHP. The versions for while and foreach loops are very similar. The only difference is the closing keywords. For a while loop, the closing keyw…
  3. 3
    One frequent pattern that we encounter is iterating over an array using a foreach loop and creating HTML elements using the items from the array. The following approach does not work as one might h…
  4. 4
    With the PHP shorthand you have just learned, you can now create more readable HTML files with embedded PHP loops. Here are the key ideas from this lesson: - The PHP shorthand for loops uses a col…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo