T
T
Theory Theory2020-05-29 19:00:30
PHP
Theory Theory, 2020-05-29 19:00:30

Similar to next in PHP generators?

function g() {
  yield 1;
  yield 2;
}

$val = g();

// как получить 1, а потом 2
// наподобие next() в javascript ?
// или в php можно только переберать через foreach ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maksim Fedorov, 2020-05-29
@Narbek

<?php

$generator = (function (): \Generator {
  yield 1;
  yield 2;
})()

var_dump($generator->current());
$generator->next();
var_dump($generator->current());

sandbox.onlinephpfunctions.com/code/f1fad691895685...
https://www.php.net/manual/en/class.generator.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question