I am currently trying to loop through a return from a function, but I just haven't been able to figure out how to do this. It's a bit like the Wordpress loop: while (have_posts()).
That is what I am trying to do, but what do my function have to return to be while-loopable?
Are you just asking what have_posts()
returns in this case?
It's not "a loopable function", it's just a function. There's nothing about it that indicates a loop. It's being used in this case as the condition for a loop:
while (have_posts())
The while
loop condition is expecting a boolean. Think of the statement as "while this condition is true, keep looping." So in this case have_posts()
should return a boolean, true or false.
As long as the function is returning true
the loop will continue. As soon as it returns false
the loop will end. The function itself has no internal knowledge of this. It's just being called over and over (each time the loop iterates).
If I understood well (your question is not very clear), you can return an array from inside your function and then do a "foreach" loop:
function num($arg) {
return array (1, 2, 3, 4);
}
foreach (num($var) as $number) {
...
}