How can pagination be implemented in PHP to improve user experience when browsing through multiple pages of content?

Implementing pagination in PHP can improve user experience when browsing through multiple pages of content by breaking up large datasets into smaller, more manageable chunks. This allows users to navigate through pages more easily and quickly find the information they are looking for.

<?php

// Assume $totalPages and $currentPage are set elsewhere in the code
$perPage = 10;
$offset = ($currentPage - 1) * $perPage;

// Query database to retrieve data for current page
$query = "SELECT * FROM your_table LIMIT $offset, $perPage";
$result = mysqli_query($connection, $query);

// Display data on the current page
while ($row = mysqli_fetch_assoc($result)) {
    // Display data here
}

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

?>