PHP 生成器 yield的使用

<?php
/**
 * Created by PhpStorm.
 * User: Itboot
 * Date: 2019/1/16
 * Time: 11:25
 */

function myGenerator()
{
    yield "hello me";
    yield "hello kunming";
    yield "hello beijing";
}

foreach (myGenerator() as $value) {
    echo $value . PHP_EOL;
}
function makeRange($len)
{
    for ($i = 0; $i < $len; $i++) {
        yield $i;
    }
}

foreach (makeRange(1000) as $value) {
    echo $value . PHP_EOL;
}

猜你喜欢

转载自blog.csdn.net/qq_34690432/article/details/86519904
今日推荐