Are there any best practices for handling pagination in PHP to ensure a smooth user experience?

When handling pagination in PHP, it is important to ensure a smooth user experience by properly managing the display of paginated data. This includes limiting the number of results per page, providing navigation links for easy page switching, and handling edge cases such as invalid page numbers or empty result sets.

// Assuming $totalPages is the total number of pages
// and $currentPage is the current page number

// Limit the number of results per page
$perPage = 10;

// Calculate the offset for the SQL query
$offset = ($currentPage - 1) * $perPage;

// Perform the SQL query with LIMIT and OFFSET clauses
$query = "SELECT * FROM table LIMIT $perPage OFFSET $offset";

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