php yield关键字

  1. 迭代生成器的用法:
<?php
function xrange($start,$end,$step=1){
    for($i = $start;$i <= $end;$i += $step){
        yield $i;
    }
}


foreach(xrange(1,10) as $num){
    echo $num.'<br/>';
}

以下是结果:

1
2
3
4
5
6
7
8
9
10

foreach是直接输出值而不是把值放到数组直接返回,这样对大数据集合操作的时候不用放到内存。

参考博客来源:在PHP中使用协程实现多任务调度

猜你喜欢

转载自blog.csdn.net/mnmnwq/article/details/79717194
今日推荐