Learn
Loops in HTML
Review
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 afor
loop - Use
endforeach
to close aforeach
loop - Use
endwhile
to close awhile
loop
- Use
- The closing keyword needs to be followed by a semicolon (
;
). - Make sure to re-enter PHP mode using
<?php
or theecho
shorthand<?=
before using PHP variables in the loop
Instructions
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.