Generator::send

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

Generator::sendSend a value to the generator

Description

public Generator::send(mixed $value): mixed

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

If the generator is not at a yield expression when this method is called, it will first be let to advance to the first yield expression before sending the value. As such it is not necessary to "prime" PHP generators with a Generator::next() call (like it is done in Python).

Parameters

value

Value to send into the generator. This value will be the return value of the yield expression the generator is currently at.

Return Values

Returns the yielded value.

Examples

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

<?php
function printer() {
    echo 
"I'm printer!".PHP_EOL;
    while (
true) {
        
$string = yield;
        echo 
$string.PHP_EOL;
    }
}

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

The above example will output:

I'm printer!
Hello world!
Bye world!

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial