Can you provide an example of using modulo division in PHP to control image layout on a webpage?

When displaying images in a grid layout on a webpage, you may want to control the number of images per row. One way to achieve this is by using modulo division to determine when to start a new row in the grid.

// Define an array of image URLs
$images = array(
    'image1.jpg',
    'image2.jpg',
    'image3.jpg',
    'image4.jpg',
    'image5.jpg',
    'image6.jpg',
    'image7.jpg',
    'image8.jpg',
    'image9.jpg',
);

// Set the number of images per row
$images_per_row = 3;

// Loop through the images array
foreach ($images as $key => $image) {
    // Check if the current index is divisible by the number of images per row
    if ($key % $images_per_row == 0) {
        echo '<div class="row">';
    }

    // Display the image
    echo '<div class="col"><img src="' . $image . '" alt="Image"></div>';

    // Close the row if the current index is the last image or divisible by the number of images per row
    if ($key == count($images) - 1 || ($key + 1) % $images_per_row == 0) {
        echo '</div>';
    }
}