for loop

Instead of brackets you can use : and endfor;, to make a loop much easier to see within HTML code

<ul>
<?php
for ($i = 0; $i < 2; $i++):
?>
<li>Item A</li>
<?php
endfor;
?>
<li>Item Z</li>
</ul>


//Or you can use like this:
<ul>
<?php for ($i = 0; $i < 2; $i++): ?>
<li>Item A</li>
<?php endfor; ?>
<li>Item Z</li>
</ul>

foreach loop

Same, but the end keyword is endforeach;

<ul>
<?php
$array = [0, 1];
foreach ($array as $i):
?>
<li>Item A</li>
<?php
endforeach;
?>
<li>Item Z</li>
</ul>

while loop

Same, but the end keyword is endwhile;

<ul>
<?php
$i = 0;
while ($i < 2):
?>
<li>Item A</li>
<?php
$i++;
endwhile;
?>
<li>Item Z</li>
</ul>
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *