What is the recommended way to handle pagination in PHP scripts to ensure smooth user experience?

When handling pagination in PHP scripts, it is recommended to use a combination of SQL queries to fetch the necessary data and display it in a paginated format on the front end. This ensures that only a subset of data is loaded at a time, improving the performance and user experience of the application.

<?php
// Set the number of items per page
$items_per_page = 10;

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

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

// Fetch data from the database using LIMIT and OFFSET
$query = "SELECT * FROM table_name LIMIT $items_per_page OFFSET $offset";
// Execute the query and display the results