Backend Development 5 min read

Understanding PHP Generator Functions: Syntax, Use Cases, and Examples

This article explains PHP generator functions, covering their purpose for memory‑efficient data iteration, streaming, and asynchronous tasks, detailing their syntax with the yield statement, providing multiple code examples, outlining advantages, usage notes, and concluding with best‑practice recommendations.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Understanding PHP Generator Functions: Syntax, Use Cases, and Examples

PHP generator functions are special functions that can produce a sequence of values without loading the entire dataset into memory.

Iterating over large data sets efficiently.

Streaming data processing.

Asynchronous programming by yielding results gradually.

Syntax of Generator Functions

In PHP a generator function is defined like any other function, but it must contain at least one yield statement.

<code>function generator() {
    // generator function code
}</code>

The yield statement pauses execution and returns a value to the caller.

Using Generator Functions

Typical usage involves defining a generator and iterating over it with foreach or other loops.

<code>// Define a generator function
function generator() {
    // first value
    yield 1;
    // second value
    yield 2;
    // third value
    yield 3;
}

// Use the generator
foreach (generator() as $value) {
    echo $value;
}</code>

This code outputs:

<code>1
2
3</code>

Advantages

Reduces memory consumption because data is generated on‑the‑fly.

Improves performance by processing items incrementally.

Simplifies code by separating iteration logic into a generator.

Examples

Iterating a data set

<code>function generator($data) {
    foreach ($data as $value) {
        yield $value;
    }
}
$data = [1, 2, 3];
foreach (generator($data) as $value) {
    echo $value;
}</code>

Outputs:

<code>1
2
3</code>

Streaming data

<code>function generator() {
    for ($i = 0; $i < 10; $i++) {
        yield $i;
    }
}
$gen = generator();
while ($value = $gen->send()) {
    echo $value;
}</code>

Outputs numbers 0 through 9.

Asynchronous task execution

<code>function generator($task) {
    $result = yield $task();
    return $result;
}
$generator = generator(function () {
    return sleep(1);
});
$result = $generator->send();
echo $result;
</code>

Outputs:

<code>1</code>

Notes

A generator must contain at least one yield statement.

It can be combined with for , foreach , or while loops for iteration.

The send() method can pass values back into the generator.

Conclusion

PHP generator functions are a powerful tool for writing efficient and flexible code; understanding their syntax and usage helps developers improve performance and memory usage.

asynchronousPHPiterationGeneratoryieldmemory-efficient
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.