锟斤拷锟斤拷锟斤拷锟斤法

一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷通锟侥猴拷锟斤拷锟斤拷锟斤拷同锟斤拷锟斤拷锟斤拷通锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷值锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷yield锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟揭拷锟街碉拷锟�

锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟矫碉拷时锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟皆憋拷锟斤拷锟斤拷锟侥讹拷锟斤拷.锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞憋拷锟�(锟斤拷锟斤拷通锟斤拷一锟斤拷foreach循锟斤拷)锟斤拷PHP 锟斤拷锟斤拷锟斤拷每锟斤拷锟斤拷要值锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷诓锟斤拷锟揭伙拷锟街抵拷蟊4锟斤拷锟斤拷锟斤拷锟斤拷锟阶刺拷锟斤拷锟斤拷锟斤拷锟斤拷涂锟斤拷锟斤拷锟斤拷锟揭拷锟斤拷锟斤拷锟揭伙拷锟街碉拷锟绞憋拷锟街革拷锟斤拷锟斤拷状态锟斤拷

一锟斤拷锟斤拷锟斤拷锟斤拷要锟斤拷锟斤拷锟斤拷锟斤拷锟街碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷约锟斤拷顺锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷拇锟斤拷牖癸拷锟斤拷约锟斤拷锟街达拷校锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷丫锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷恕锟�

Note:

一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟皆凤拷锟斤拷值锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷然锟斤拷return锟斤拷锟斤拷一锟斤拷锟斤拷效锟斤拷锟斤法锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷止锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷执锟叫★拷

yield锟截硷拷锟斤拷

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥猴拷锟斤拷锟斤拷yield锟截硷拷锟街★拷锟斤拷锟斤拷虻サ牡锟斤拷锟斤拷锟绞斤拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟絩eturn锟斤拷锟斤拷锟斤拷锟斤拷同之锟斤拷锟斤拷锟斤拷锟斤拷通return锟结返锟斤拷值锟斤拷锟斤拷止锟斤拷锟斤拷锟斤拷执锟叫o拷锟斤拷yield锟结返锟斤拷一锟斤拷值锟斤拷循锟斤拷锟斤拷锟矫达拷锟斤拷锟斤拷锟斤拷锟侥达拷锟诫并锟斤拷只锟斤拷锟斤拷停执锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷

Example #1 一锟斤拷锟津单碉拷锟斤拷锟斤拷值锟斤拷锟斤拷锟斤拷

<?php
function gen_one_to_three() {
    for (
$i 1$i <= 3$i++) {
        
//注锟斤拷锟斤拷锟�$i锟斤拷值锟节诧拷同锟斤拷yield之锟斤拷锟角憋拷锟街达拷锟捷的★拷
        
yield $i;
    }
}

$generator gen_one_to_three();
foreach (
$generator as $value) {
    echo 
"$value\n";
}
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

1
2
3

Note:

锟斤拷锟节诧拷锟斤拷为锟斤拷锟缴碉拷值锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷枪锟斤拷锟斤拷锟斤拷锟斤拷椤�

Caution

锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷式锟斤拷锟斤拷锟斤拷(锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷值锟斤拷锟绞斤拷锟斤拷也锟�)锟斤拷使锟斤拷yield锟斤拷锟斤拷锟斤拷锟绞癸拷锟皆诧拷锟斤拷虐锟統ield锟斤拷锟斤拷锟斤拷围锟斤拷锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷效锟侥o拷

$data = (yield $value);

锟斤拷锟斤拷锟斤拷锟酵诧拷锟较凤拷锟斤拷锟斤拷锟斤拷锟斤拷PHP5锟叫伙拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷

$data = yield $value;

The parenthetical restrictions do not apply in PHP 7.

锟斤拷锟斤拷锓拷锟斤拷院锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷Generator::send()锟斤拷锟斤拷锟斤拷锟绞癸拷谩锟�

指锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值

PHP锟斤拷锟斤拷锟斤拷支锟街癸拷锟斤拷锟斤拷值锟斤拷锟斤拷锟介,锟斤拷锟斤拷锟斤拷也一锟斤拷支锟街★拷锟斤拷锟皆筹拷锟斤拷锟斤拷锟缴简单碉拷值锟斤拷锟斤拷也锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷时锟斤拷指锟斤拷锟斤拷锟斤拷锟斤拷

锟斤拷锟斤拷锟斤拷示锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷值锟斤拷锟诫定锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷十锟斤拷锟斤拷锟狡★拷

Example #2 锟斤拷锟斤拷一锟斤拷锟斤拷值锟斤拷

<?php
/* 
 * 锟斤拷锟斤拷每一锟斤拷锟斤拷锟矫分号分革拷锟斤拷侄锟斤拷锟较o拷锟斤拷一锟斤拷锟街段斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
 */

$input = <<<'EOF'
1;PHP;Likes dollar signs
2;Python;Likes whitespace
3;Ruby;Likes blocks
EOF;

function 
input_parser($input) {
    foreach (
explode("\n"$input) as $line) {
        
$fields explode(';'$line);
        
$id array_shift($fields);

        yield 
$id => $fields;
    }
}

foreach (
input_parser($input) as $id => $fields) {
    echo 
"$id:\n";
    echo 
"    $fields[0]\n";
    echo 
"    $fields[1]\n";
}
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

1:
    PHP
    Likes dollar signs
2:
    Python
    Likes whitespace
3:
    Ruby
    Likes blocks
Caution

锟斤拷之前锟斤拷锟缴硷拷值锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟绞斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷杉锟街碉拷锟揭诧拷锟揭癸拷锟皆诧拷锟斤拷沤锟斤拷邪锟轿э拷锟�

$data = (yield $key => $value);

锟斤拷锟斤拷null值

Yield锟斤拷锟斤拷锟斤拷没锟叫诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟铰憋拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷 NULL值锟斤拷锟斤拷锟揭伙拷锟斤拷远锟斤拷募锟斤拷锟斤拷锟�

Example #3 锟斤拷锟斤拷NULLs

<?php
function gen_three_nulls() {
    foreach (
range(13) as $i) {
        yield;
    }
}

var_dump(iterator_to_array(gen_three_nulls()));
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

array(3) {
  [0]=>
  NULL
  [1]=>
  NULL
  [2]=>
  NULL
}

使锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值

锟斤拷锟缴猴拷锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷值一锟斤拷锟斤拷使锟斤拷锟斤拷锟斤拷锟斤拷锟缴★拷锟斤拷锟斤拷锟�returning references from functions锟斤拷锟接猴拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟矫o拷一锟斤拷锟斤拷通锟斤拷锟节猴拷锟斤拷锟斤拷前锟斤拷锟揭伙拷锟斤拷锟斤拷梅锟斤拷拧锟�

Example #4 使锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值

<?php
function &gen_reference() {
    
$value 3;

    while (
$value 0) {
        yield 
$value;
    }
}

/* 
 * 锟斤拷锟角匡拷锟斤拷锟斤拷循锟斤拷锟斤拷锟睫革拷$number锟斤拷值锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷使锟矫碉拷锟斤拷锟斤拷值锟斤拷锟斤拷锟缴o拷锟斤拷锟斤拷gen_reference()锟节诧拷锟斤拷$value值也锟斤拷锟斤拷疟浠拷锟�
 */
foreach (gen_reference() as &$number) {
    echo (--
$number).'... ';
}
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

2... 1... 0... 

Generator delegation via yield from

In PHP 7, generator delegation allows you to yield values from another generator, Traversable object, or array by using the yield from keyword. The outer generator will then yield all values from the inner generator, object, or array until that is no longer valid, after which execution will continue in the outer generator.

If a generator is used with yield from, the yield from expression will also return any value returned by the inner generator.

Example #5 Basic use of yield from

<?php
function count_to_ten() {
    yield 
1;
    yield 
2;
    yield from [
34];
    yield from new 
ArrayIterator([56]);
    yield from 
seven_eight();
    yield 
9;
    yield 
10;
}

function 
seven_eight() {
    yield 
7;
    yield from 
eight();
}

function 
eight() {
    yield 
8;
}

foreach (
count_to_ten() as $num) {
    echo 
"$num ";
}
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

1 2 3 4 5 6 7 8 9 10 

Example #6 yield from and return values

<?php
function count_to_ten() {
    yield 
1;
    yield 
2;
    yield from [
34];
    yield from new 
ArrayIterator([56]);
    yield from 
seven_eight();
    return yield from 
nine_ten();
}

function 
seven_eight() {
    yield 
7;
    yield from 
eight();
}

function 
eight() {
    yield 
8;
}

function 
nine_ten() {
    yield 
9;
    return 
10;
}

$gen count_to_ten();
foreach (
$gen as $num) {
    echo 
"$num ";
}
echo 
$gen->getReturn();
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

1 2 3 4 5 6 7 8 9 10