How can the PHP script be optimized to improve readability and efficiency in counting and displaying images per page?

To optimize the PHP script for counting and displaying images per page, you can use a loop to iterate through the images array and count them. Additionally, you can implement pagination to limit the number of images displayed per page, improving efficiency.

<?php
// Assuming $images is an array of image filenames
$imagesPerPage = 10;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$start = ($page - 1) * $imagesPerPage;
$end = $start + $imagesPerPage;

$totalImages = count($images);
$totalPages = ceil($totalImages / $imagesPerPage);

echo "Total Images: $totalImages<br>";
echo "Page $page of $totalPages<br>";

for ($i = $start; $i < $end && $i < $totalImages; $i++) {
    echo "<img src='images/{$images[$i]}' alt='Image $i'><br>";
}

// Pagination links
for ($i = 1; $i <= $totalPages; $i++) {
    echo "<a href='?page=$i'>$i</a> ";
}
?>