Generator
PHP Manual

Generator::send

(PHP 5 >= 5.5.0)

Generator::sendSend a value to the generator

Descrição

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.

Parâmetros

value

Exemplos

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

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

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

O exemplo acima irá imprimir:

Hello world!

Valor Retornado

Returns the yielded value.


Generator
PHP Manual