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