What are some best practices for handling pagination in PHP when displaying database records?

When displaying a large number of database records in a web application, pagination is essential to improve performance and user experience. To implement pagination in PHP, you can use SQL queries to limit the number of records fetched from the database and display them in multiple pages.

<?php
// Define the number of records to display per page
$records_per_page = 10;

// Calculate the total number of pages
$total_records = // query to get total number of records from database;
$total_pages = ceil($total_records / $records_per_page);

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

// Calculate the starting record for the current page
$start = ($current_page - 1) * $records_per_page;

// Query to fetch records for the current page
$query = "SELECT * FROM table_name LIMIT $start, $records_per_page";
$result = // execute the query;

// Display the records
while ($row = mysqli_fetch_assoc($result)) {
    // Display the record data
}

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