0
0
PHPprogramming~3 mins

Why Memory efficiency with generators in PHP? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could process huge data without your program ever slowing down or crashing?

The Scenario

Imagine you have a huge list of data, like millions of user records, and you want to process them one by one.

If you try to load all this data into memory at once, your program might slow down or even crash.

The Problem

Loading everything into memory uses a lot of space and can make your program very slow.

It's like trying to carry all your groceries in one trip when you only have small bags -- it's heavy and hard to manage.

The Solution

Generators let you handle one item at a time without loading everything at once.

This saves memory and keeps your program fast and smooth, like carrying groceries one bag at a time.

Before vs After
Before
$data = file('bigfile.txt');
foreach ($data as $line) {
    process($line);
}
After
function readLines($file) {
    $handle = fopen($file, 'r');
    if ($handle) {
        while (($line = fgets($handle)) !== false) {
            yield $line;
        }
        fclose($handle);
    }
}

foreach (readLines('bigfile.txt') as $line) {
    process($line);
}
What It Enables

You can work with huge data sets easily without worrying about running out of memory.

Real Life Example

Processing large log files line by line to find errors without loading the entire file into memory.

Key Takeaways

Loading all data at once can crash your program.

Generators let you handle data one piece at a time.

This saves memory and keeps your program fast.