What are the best practices for handling pagination in PHP when displaying large amounts of data?

When displaying large amounts of data in PHP, it is important to implement pagination to improve performance and user experience. This involves breaking up the data into smaller chunks or pages, allowing users to navigate through the content more easily. To handle pagination effectively, you can use SQL queries with LIMIT and OFFSET clauses to fetch only a subset of data at a time.

<?php

// Define the number of items per page
$itemsPerPage = 10;

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

// Calculate the offset for the SQL query
$offset = ($page - 1) * $itemsPerPage;

// Query to fetch data with pagination
$sql = "SELECT * FROM your_table LIMIT $itemsPerPage OFFSET $offset";

// Execute the query and display the data
// (Code to execute the query and display data goes here)

// Pagination links
$totalPages = ceil($totalItems / $itemsPerPage);
for ($i = 1; $i <= $totalPages; $i++) {
    echo "<a href='?page=$i'>$i</a> ";
}

?>