In what ways can basic mathematical operations be applied in PHP to determine the pages to display before and after the current page in a pagination system?

To determine the pages to display before and after the current page in a pagination system, basic mathematical operations can be applied in PHP. By calculating the total number of pages, the current page number, and the number of pages to display before and after the current page, we can determine which page numbers to show in the pagination navigation.

// Assuming $totalPages, $currentPage, $pagesBeforeCurrent, and $pagesAfterCurrent are already defined

$startPage = max(1, $currentPage - $pagesBeforeCurrent);
$endPage = min($totalPages, $currentPage + $pagesAfterCurrent);

for ($i = $startPage; $i <= $endPage; $i++) {
    echo '<a href="?page=' . $i . '">' . $i . '</a>';
}