UrbanDweller Posted November 2, 2011 Share Posted November 2, 2011 Hey Before I start playing around with some code I want to know if it is possible to call a function inside a while loop that will call echo a some stuff then return to the loop it got called by? thanks Quote Link to comment https://forums.phpfreaks.com/topic/250262-using-a-function-inside-a-loop/ Share on other sites More sharing options...
manohoo Posted November 2, 2011 Share Posted November 2, 2011 The answer is yes, a few minutes ago I posted this for someone who wanted to simulate the lottery: $numbers = array(); function draw() { return rand(1,59); } for ($i=0; sizeof($numbers) < 6; $i++) { $new = draw(); if (in_array($new, $numbers)) { $new = draw (); } else { $numbers[] = $new; } } var_dump($numbers); Quote Link to comment https://forums.phpfreaks.com/topic/250262-using-a-function-inside-a-loop/#findComment-1284123 Share on other sites More sharing options...
Anti-Moronic Posted November 2, 2011 Share Posted November 2, 2011 Good example, but the trouble with that is if the draw() number exists it will generate another draw() number without verifying if that also exists. Would be better to define the numbers and have them removed from a number map so they cannot possibly re-occur. Of course, there may also be a better way.. Quote Link to comment https://forums.phpfreaks.com/topic/250262-using-a-function-inside-a-loop/#findComment-1284179 Share on other sites More sharing options...
kicken Posted November 2, 2011 Share Posted November 2, 2011 I don't know about better, but this is the way I code things like the example given: $numbers = array(); function draw() { return rand(1,59); } while (count($numbers) < 6){ do { $new = draw(); } while (in_array($new, $numbers)); $numbers[] = $new; } var_dump($numbers); @UrbanDweller functions don't care where they are called from, they still work the same. when you call a function the code jumps there, runs through the function, then jumps back to where it was called from at the end/return of the function. Quote Link to comment https://forums.phpfreaks.com/topic/250262-using-a-function-inside-a-loop/#findComment-1284372 Share on other sites More sharing options...
xyph Posted November 2, 2011 Share Posted November 2, 2011 Perhaps just <?php $amount = 5; $numbers = range( 1,100 ); shuffle( $numbers ); $lottery = array_splice( $numbers, 0, $amount ); print_r( $lottery ); ?> But this is not what the OP wanted. The answer is yes. Why not try it out rather than asking? Quote Link to comment https://forums.phpfreaks.com/topic/250262-using-a-function-inside-a-loop/#findComment-1284374 Share on other sites More sharing options...
UrbanDweller Posted November 3, 2011 Author Share Posted November 3, 2011 Thanks for that, one extra thing is how can i send a variable made inside the executed function back to the parent function loop without resetting the while loop? Quote Link to comment https://forums.phpfreaks.com/topic/250262-using-a-function-inside-a-loop/#findComment-1284555 Share on other sites More sharing options...
phporcaffeine Posted November 3, 2011 Share Posted November 3, 2011 Thanks for that, one extra thing is how can i send a variable made inside the executed function back to the parent function loop without resetting the while loop? While technically possible, you would nee to be very, very careful. Quote Link to comment https://forums.phpfreaks.com/topic/250262-using-a-function-inside-a-loop/#findComment-1284560 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.