Understanding PHP Generators

Understanding PHP Generators

PHP generators provide a powerful and efficient way to handle iteration without the need to create and store the entire dataset in memory. This feature is particularly useful when working with large data sets or streams of data.

What is a Generator?

A generator is a special type of function that can be paused and resumed during execution, allowing you to generate a sequence of values over time rather than computing them all at once and sending them back.

How Generators Work

Generators use the yield keyword to return values one at a time. When a generator function is called, it returns an object that can be iterated over. Unlike normal functions, which return a single value and terminate, a generator can yield multiple values over its execution.

Here's a basic example:

function myGenerator() {
    yield 'First';
    yield 'Second';
    yield 'Third';

foreach (myGenerator() as $value) {
    echo $value, PHP_EOL;



In this example, the generator function myGenerator yields three values. When iterated over, it outputs each value one at a time.

Benefits of Using Generators

  1. Memory Efficiency: Generators use less memory because they generate values on the fly, rather than storing the entire dataset in memory.

  2. Lazy Evaluation: Values are computed only when needed, which is useful for handling large or infinite sequences.

  3. Simplified Code: Generators can make your code cleaner and easier to read by abstracting iteration logic.

Practical Examples

  1. Reading Large Files

When reading a large file, you can use a generator to process it line by line without loading the entire file into memory.

function readFileLines($filePath) {
    $file = fopen($filePath, 'r');
    while ($line = fgets($file)) {
        yield $line;

foreach (readFileLines('largefile.txt') as $line) {
    echo $line;
  1. Generating Infinite Sequences

Generators can create infinite sequences efficiently.

function infiniteSequence() {
    $i = 0;
    while (true) {
        yield $i++;

foreach (infiniteSequence() as $number) {
    if ($number > 10) break;
    echo $number, PHP_EOL;
  1. Processing Database Results

When querying a database, you can fetch results in batches using a generator.

function fetchDatabaseRows(PDO $pdo, $query) {
    $stmt = $pdo->query($query);
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        yield $row;

// Usage
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');
foreach (fetchDatabaseRows($pdo, 'SELECT * FROM large_table') as $row) {


PHP generators are a valuable tool for handling large datasets or streams of data efficiently. By using the yield keyword, generators allow you to write code that is both memory-efficient and easy to read. Whether you’re working with large files, infinite sequences, or database results, generators can help you manage data in a more effective way. Explore the use of PHP generators in your projects to see how they can optimize your code.

Happy coding!