php5.5新特性通过yield来逐行读取大文件

php 代码:

[code lang=”php”]
function getLines($path)
{
$f = fopen($path,’r’);
while(!feof($f))
{
$data = fgets($f);
yield $data;
}

fclose($f);

}

foreach(getLines($path) as $line=>$val)
{
if($line>100000) break;
echo $val."<br />";
}
[/code]