<?php
function get_val1($n = 10000*100)
{
$arr = [];
for($i = 0; $i <= $n;$i++){
$arr[] = $i;
if(($i % 100000) == 0){
echo round(memory_get_usage() / 1024 / 1024 ,2) . " MB<br>";
}
}
return $arr;
}
function get_val2($n = 100)
{
for($i = 0; $i <= $n;$i++){
yield $i;
if(($i % 10) == 0){
echo round(memory_get_usage() / 1024 / 1024 ,2) . " MB<br>";
}
}
}
$a = get_val2();
foreach($a as $k => $v){
// echo "$v <br>";
}
//使用生成器来读取文件
function readTxt()
{
//1.打开文件
$file = fopen('./a.txt','r');
//2.读取文件
while (!feof($file)){
yield fgets($file , 1024);
}
//3.关闭文件
fclose($file);
}
$content = readTxt();
foreach($content as $v){
echo $v . '<br>';
}
function gen(){
$id = 2;
$id = yield $id;
echo $id;
}
$gen = gen();
$gen->send(1000);