How can PHP be optimized to handle large datasets without running into memory issues?

When dealing with large datasets in PHP, memory issues can arise due to the limited memory allocated to PHP scripts. One solution is to process the data in smaller chunks rather than loading the entire dataset into memory at once. This can be achieved by using techniques like pagination or streaming data processing.

// Example code showing how to process large dataset in smaller chunks using pagination

// Define the chunk size
$chunkSize = 1000;

// Query to fetch data from database
$query = "SELECT * FROM large_table";

// Get total number of rows
$totalRows = $db->query($query)->num_rows;

// Calculate number of chunks
$numChunks = ceil($totalRows / $chunkSize);

for ($i = 0; $i < $numChunks; $i++) {
    $offset = $i * $chunkSize;
    
    // Query database with limit and offset
    $result = $db->query($query . " LIMIT $chunkSize OFFSET $offset");
    
    // Process the data
    while ($row = $result->fetch_assoc()) {
        // Do something with the data
    }
}