Is it possible to return a loop? not the result but the loop it self. I want to create a function in php. For example like this.
function myloop($sql){
$quer
In PHP 5.5+ it is possible, using the yield
keyword instead of return
(this is called a generator):
function myloop($sql) {
$query = mysql_query($sql);
while (($row = mysql_fetch_assoc($query))) {
yield $row;
}
}
foreach (myloop('SELECT * FROM foo') as $row) {
// do something with $row
}
This is not much different from what you could do with iterators in older PHP, but the code is much cleaner.