Multiple includes in a foreach loop
3 posts by 2 authors in: Forums > CMS Builder
Last Post: October 19, 2020 (RSS)
By Toledoh - October 18, 2020
Hi All.
Can anyone explain to me why the first option includes _panelContent.php only once, but the second option includes multiples? (I need multiples)
Option 1:
<?php foreach ($panelsRecords as $record): ?>
<?php include("_panelContent.php") ?>
<?php endforeach ?>
Option 2:
<?php foreach ($panelsRecords as $record); include("_panelContent.php"); ?>
Tim (toledoh.com.au)
By Carl - October 19, 2020 - edited: October 19, 2020
Hi Toledoh,
With Option 1 you use a colon. The equivalent PHP to this is
<?php foreach ($panelsRecords as $record){ ?>
<?php include("_panelContent.php") ?>
<?php } ?>
For Option 2, you're using a semi-colon which terminates the loop right away. The equivalent to this is:
<?php foreach ($panelsRecords as $record){ } include("_panelContent.php"); ?>
PHP Programmer
interactivetools.com