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 colon (:) instead of a bracket ({) to open the code block.
  • The shorthand uses keywords to close the code block instead of a bracket (}):
    • Use endfor to close a for loop
    • Use endforeach to close a foreach loop
    • Use endwhile to close a while loop
  • The closing keyword needs to be followed by a semicolon (;).
  • Make sure to re-enter PHP mode using <?php or the echo shorthand <?= before using PHP variables in the loop


We’ve given an example of a for, foreach, and while loop using the PHP shorthand. Each example also uses the echo shorthand to add PHP variable values to the HTML.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?