How can you implement pagination without using a database in PHP?

When implementing pagination without using a database in PHP, you can store your data in an array and then use PHP to paginate through the array based on the current page and number of items per page. By keeping track of the current page and calculating the offset, you can display the appropriate subset of data on each page.

<?php
// Sample data stored in an array
$data = range(1, 100);

// Pagination variables
$items_per_page = 10;
$current_page = isset($_GET['page']) ? $_GET['page'] : 1;
$total_items = count($data);
$total_pages = ceil($total_items / $items_per_page);
$offset = ($current_page - 1) * $items_per_page;

// Get subset of data for current page
$subset = array_slice($data, $offset, $items_per_page);

// Display data on current page
foreach ($subset as $item) {
    echo $item . "<br>";
}

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