9951 explained code solutions for 126 technologies


twigHow to use the 'for' loop with PHP and Twig?


The for loop is a powerful tool for iterating over a set of data in PHP and Twig. It allows you to execute a block of code for each item in a set of data.

Example code

{% for item in items %}
  <p>{{ item }}</p>
{% endfor %}

Output example

<p>Item 1</p>
<p>Item 2</p>
<p>Item 3</p>

Code explanation

  • for: the keyword used to start the loop
  • item: the variable used to store the current item in the loop
  • items: the set of data being looped over
  • endfor: the keyword used to end the loop

Helpful links

Edit this code on GitHub