How can PHP developers limit the number of displayed page numbers in a pagebar to only show the first three and last three pages?

To limit the number of displayed page numbers in a pagebar to only show the first three and last three pages, PHP developers can achieve this by checking the current page number and adjusting the range of displayed page numbers accordingly. This can be done by dynamically calculating the start and end page numbers to show based on the current page number and the total number of pages.

<?php
$current_page = 5; // Current page number
$total_pages = 10; // Total number of pages

$start_page = max(1, $current_page - 2); // Calculate start page number
$end_page = min($total_pages, $current_page + 2); // Calculate end page number

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