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: 14 July 2026