- str_repeat: documentation ( source)
- memory_get_usage: documentation ( source)
<?php
class A1
{
protected function a()
{
$i = 0;
$s = '';
while (true) {
$i++;
if ($i > 10000) {
break;
}
foreach ([['a' => 1], 2, ['a' => 2]] as $p) {
if ($p == 2) {
continue;
}
if ($i % 100 == 0) {
echo $i, memory_get_usage(), PHP_EOL;
try {
$s .= str_repeat('0123456789', 100000);
} catch (\Exception $e) {
throw $e;
}
}
yield $p['a'] => 123;
}
}
}
public function b()
{
$sum = 0;
try {
foreach ($this->a() as $k => $v) {
$sum += $k + $v;
}
echo 'Done: ', $sum;
} catch (\Exception $e) {
echo $e;
}
}
}
(new A1)->b();