Generators advanced

Contents
Send values in
Return from generator
Yield from
Keys

Send values in

By default data flows one way from yield to the loop. Calling send pushes a value back into the paused generator where yield works as an expression.

The value passed to send becomes the result of the current yield, so the generator can react to the caller on every step.

function echoer(): Generator { $input = yield "ready"; while (true) { $input = yield "got " . $input; } } $gen = echoer(); $gen->rewind(); echo $gen->current(); $gen->send("hello"); echo $gen->current();

Return from generator

A generator can finish with a return value in addition to yielded items. The loop still ends normally and then getReturn reveals the final result.

This pattern fits totals, counts, and status codes computed while streaming values one by one.

function countUp(int $max): Generator { for ($i = 1; $i <= $max; $i++) { yield $i; } return $max * 10; } $gen = countUp(3); foreach ($gen as $n) { echo $n; } echo $gen->getReturn();

Yield from

Yield from delegates part of the work to another generator. Values flow through as if written inline, which keeps large sequences split into small readers.

The outer generator pauses while the inner one runs, then continues after the last inner value.

function inner(): Generator { yield 1; yield 2; } function outer(): Generator { yield 0; yield from inner(); yield 3; } foreach (outer() as $v) { echo $v; }

Keys

Generators can yield keys together with values, just like arrays. String keys name each item, which makes logs and mapped data easier to follow.

Functions like iterator to array preserve those keys, so downstream code can stay unchanged.

function pairs(): Generator { yield "a" => 1; yield "b" => 2; } foreach (pairs() as $k => $v) { echo $k; echo $v; } var_dump(iterator_to_array(pairs()));

Next: Fibers (PHP 8.1)

Article author: Arthur Isaev

Related articles
Development with PHP
PHP course: zero to hero in 50 lessons
What is PHP and where it runs
Install PHP 8.5 and run your first command
Your first PHP script
Comments in PHP
Variables and constants
Types in PHP
Strings in PHP
Numbers in PHP
Operators
if and else
switch and match
Ternary, null coalescing and nullsafe
Loops
Arrays
Associative arrays
Functions
Variable scope
include and require
Namespaces
Composer and autoloading
Everyday string functions
Dates and time
JSON in PHP
Reading and writing files
Errors: read, reproduce, fix
Classes and objects
Constructors and promotion
Inheritance
Interfaces and traits
Enums
Magic methods
Attributes
Exceptions
Strict types
Closures and arrow functions
Generators with yield
Forms: GET and POST
HTTP: headers and status codes
Cookies
Sessions
Login with sessions
JSON REST endpoint
Database with PDO
Uploads and .htaccess
Lint like a pro
Clean functions
Debugging
Modern PHP tour: 5 to 8.4
PHP 8.5 for busy developers
Hero roadmap: the whole course on one page
Install PHP 8.5 on Windows 11
Install PHP 8.5 on Ubuntu
Install PHP 8.5 on Rocky Linux
Install PHP 8.5 on macOS
Install PHP 8.5 on FreeBSD
PHP 8.5 in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: