What are some best practices for optimizing pagination functionality in PHP applications to enhance user experience?

When implementing pagination in PHP applications, it is essential to optimize the functionality to enhance user experience. One best practice is to limit the number of records fetched from the database to improve performance. Additionally, providing clear navigation controls for users to easily move between pages can make the pagination more user-friendly.

// Example code snippet for optimizing pagination functionality in PHP

// Set the number of records to display per page
$records_per_page = 10;

// Calculate the offset for fetching records from the database
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$offset = ($page - 1) * $records_per_page;

// Fetch records from the database using LIMIT and OFFSET
$query = "SELECT * FROM table_name LIMIT $records_per_page OFFSET $offset";
$result = mysqli_query($connection, $query);

// Display pagination links for navigation
$total_records = // Get total number of records from the database
$total_pages = ceil($total_records / $records_per_page);

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