What is the best practice for implementing a pagination feature in a PHP application?

When implementing a pagination feature in a PHP application, it is best practice to limit the number of records fetched from a database at a time to improve performance and user experience. This can be achieved by using the LIMIT clause in SQL queries along with calculating the total number of pages based on the total number of records and the desired number of records per page.

// Assuming $currentPage and $recordsPerPage are already defined
$offset = ($currentPage - 1) * $recordsPerPage;
$query = "SELECT * FROM table_name LIMIT $offset, $recordsPerPage";
// Execute the query and fetch results

// Calculate total number of pages
$totalRecords = // Get total number of records from database
$totalPages = ceil($totalRecords / $recordsPerPage);

// Display pagination links based on $totalPages and $currentPage