PHP 7.1.20 Released

Generator::current

(PHP 5 >= 5.5.0, PHP 7)

Generator::currentGet the yielded value

Description

public mixed Generator::current ( void )

Parameters

This function has no parameters.

Return Values

Returns the yielded value.

add a note add a note

User Contributed Notes 1 note

up
1
mrsoftware73 at gmail dot com
5 months ago
<?php
function gen_one_to_three() {
    for (
$i = 1; $i <= 3; $i++) {
       
// Note that $i is preserved between yields.
       
yield $i;
    }
}

$generator = gen_one_to_three();
foreach (
$generator as $value) {
    echo
"regular : ".$value , PHP_EOL;
    echo
"With current function : ".$generator->current(),PHP_EOL;
}
?>
To Top