switch ($texan) { case "y'all": return "some of you"; case "all y'all": return "all of you"; default: return $texan; }
function gRange($min, $max, $step): \Generator { for ($i = $min; $i < $max; $i += $step) { yield $i; } } foreach (gRange(12, 97, 5) as $n) { echo "$n\n"; // 12, 17, 22, etc. }
function spooky(): \Generator { while (true) { yield random_int(0, 1) ? 'O' : 'o'; } } foreach (spooky() as $chr) { echo $chr; usleep(100000); }
If you call a function that yields, you will not execute that function. You'll get back a generator. To execute the function, you (or the event loop) will interact with that generator.
Also, "return" means something different for a generator than for a normal function.
$g = gen(1);
$a = $g->current();
2
$b = yield $arg1 + 1;
$c = $g->send($a + 1);
$d = yield $b + 2;
5
3
function gen($arg1)
$e = $g->send($c + 1);
6
return $d + 2;
null
echo $g->getReturn();
8
* Ignoring curl_multi and wrappers (e.g. Guzzle)
var_dump(is_12_factor()); // bool(true)
* Throwables to the rescue!
** amphp/cluster to the rescue!