What potential issues can arise when calculating the start and end pages for pagination in PHP?

One potential issue that can arise when calculating start and end pages for pagination in PHP is when the total number of pages is less than the number of pages to display. This can result in incorrect pagination links being displayed. To solve this issue, you can adjust the start and end pages based on the total number of pages.

// Calculate start and end pages for pagination
$totalPages = 10;
$currentPage = 5;
$pagesToShow = 5;

$startPage = max(1, $currentPage - floor($pagesToShow / 2));
$endPage = min($totalPages, $startPage + $pagesToShow - 1);

// Adjust start and end pages if total pages are less than pages to display
if ($endPage - $startPage + 1 < $pagesToShow) {
    $startPage = max(1, $totalPages - $pagesToShow + 1);
}

echo "Start Page: " . $startPage . "<br>";
echo "End Page: " . $endPage;