Generator
PHP Manual

Generator::send

(PHP 5 >= 5.5.0)

Generator::sendSend a value to the generator

Description

public mixed Generator::send ( mixed $value )

Sends the given value to the generator as the result of the yield expression and resumes execution of the generator.

Generator::send() allows values to be injected into generator functions while iterating over them. The injected value will be returned from the yield statement and can then be used like any other variable within the generator function.

Parameters

value

Examples

Example #1 Using Generator::send() to inject values

<?php
function printer() {
    while (
true) {
        
$string yield;
        echo 
$string;
    }
}

$printer printer();
$printer->send('Hello world!');
?>

The above example will output:

Hello world!

Return Values

Returns the yielded value.


Generator
PHP Manual