I am trying to limit the iterations of a foreach loop to display only 4 items contained in a folder, instead of all, however at the moment it keeps displaying all items:
<?php
$dir = 'images';
$images = scandir($dir);
$i=0;
foreach($images as $file){
if ($i == 4) {
break;
}
$pos = strpos($file, '-s.jpg');
if ($pos !== false) {
echo '<a href="/images/'.$file.'"><img src="/images/'.$file.'"/></a>';
} // end if strpos
$i++;
} // end foreach
?>