What is the logic behind limiting the number of displayed pages to the current page plus or minus four in the PHP script?

Limiting the number of displayed pages to the current page plus or minus four in a PHP script helps to prevent overwhelming users with too many page navigation links, especially in cases where there are a large number of pages. This approach provides a more user-friendly experience by only showing a manageable range of page links around the current page.

// Get the current page number
$current_page = isset($_GET['page']) ? $_GET['page'] : 1;

// Calculate the range of pages to display
$start_page = max(1, $current_page - 4);
$end_page = min($start_page + 8, $total_pages);

// Display the page links within the range
for ($i = $start_page; $i <= $end_page; $i++) {
    echo '<a href="?page=' . $i . '">' . $i . '</a>';
}