How can the pagination functionality be optimized for better performance in PHP?

When dealing with pagination in PHP, it's important to optimize the functionality for better performance by limiting the amount of data fetched from the database and minimizing the number of database queries. One way to achieve this is by using the LIMIT clause in SQL queries to only fetch the necessary data for each page. Additionally, caching the results of database queries can help improve performance by reducing the number of times the same data is retrieved.

// Example of implementing pagination with optimized performance in PHP

// Calculate the offset based on the current page and number of items per page
$offset = ($current_page - 1) * $items_per_page;

// Fetch data from the database with LIMIT clause to only retrieve necessary data
$query = "SELECT * FROM table_name LIMIT $offset, $items_per_page";
$result = mysqli_query($connection, $query);

// Display the data fetched from the database
while ($row = mysqli_fetch_assoc($result)) {
    // Display data here
}