Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Send An Argument To Generator

By Rahul Chavan

<?php

function checkEvenOrOdd() {
    $value = yield;
    
    yield ($value % 2 === 0) ? "Even: $value" : "Odd: $value";
}

$evenOddChecker = checkEvenOrOdd();

echo $evenOddChecker->send(7). PHP_EOL;

When calling the method send() on a PHP generator, the result of the first yield keyword is the argument value. Then, it can be used to yield a specific result.

Upon usage on a foreach(), that generator will generate 2 values.

Generator::send() only accepts one argument.

See Also

PHP Features

Last updated: 10 September 2026