What are some best practices for efficiently handling large datasets in PHP, such as the example with 400,000 entries?

Handling large datasets in PHP efficiently involves techniques like pagination, using database indexes, optimizing queries, and caching results. One approach is to limit the number of records fetched at a time and implement pagination to reduce memory usage and processing time.

// Example of fetching and displaying large dataset with pagination

// Establish database connection
$pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');

// Set pagination variables
$limit = 100; // Number of records to fetch per page
$page = isset($_GET['page']) ? $_GET['page'] : 1; // Current page number

// Calculate offset
$offset = ($page - 1) * $limit;

// Fetch records with limit and offset
$stmt = $pdo->prepare("SELECT * FROM table_name LIMIT :limit OFFSET :offset");
$stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$records = $stmt->fetchAll();

// Display records
foreach ($records as $record) {
    echo $record['column_name'] . "<br>";
}

// Pagination links
$total_records = $pdo->query("SELECT COUNT(*) FROM table_name")->fetchColumn();
$total_pages = ceil($total_records / $limit);

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