jlryan87 Posted April 22, 2007 Share Posted April 22, 2007 Hello everyone. Today I make a code to display numbers 1-20 randomly without repeating any of the numbers. The result is fine but I keep on getting Fatal error: Maximum execution time of 30 seconds exceeded. Personally I don't think PHP will have much trouble going through the loop. Here's the code I have written: <? $i = 0; $m = 20; while ($i<$m){ $n = rand(1,20); if (ereg(" ".$n." ", $k) == 1){ $i -=1; }else{ $k .= " ".$n." "; echo " $n "; $i +=1; } } ?> Is there any way I can write the code without having the fatal error? What is wrong with my code? There's no infinite loop and I don't think there is a long loop either. Thanks everyone who gives some help. Link to comment https://forums.phpfreaks.com/topic/48148-fatal-error-maximum-execution-time-of-30-seconds-exceeded/ Share on other sites More sharing options...
Janus13 Posted April 22, 2007 Share Posted April 22, 2007 One test you can do to make sure you aren't in an infinite loop would be to echo out each number as it's reached in your loop. Outside of that possibility, I haven't seen that particular error yet. Perhaps someone else has. Link to comment https://forums.phpfreaks.com/topic/48148-fatal-error-maximum-execution-time-of-30-seconds-exceeded/#findComment-235327 Share on other sites More sharing options...
Navarr Posted April 22, 2007 Share Posted April 22, 2007 http://www.php.net/manual/en/function.set-time-limit.php <?php set_time_limit(0); ?> Link to comment https://forums.phpfreaks.com/topic/48148-fatal-error-maximum-execution-time-of-30-seconds-exceeded/#findComment-235330 Share on other sites More sharing options...
ertarunz7 Posted November 18, 2009 Share Posted November 18, 2009 Just remove $i -=1; and its done .. You dont need to apply -1 here . Link to comment https://forums.phpfreaks.com/topic/48148-fatal-error-maximum-execution-time-of-30-seconds-exceeded/#findComment-959830 Share on other sites More sharing options...
Mark Baker Posted November 18, 2009 Share Posted November 18, 2009 Alternatively, why not simply use $numbers = range(1,20); shuffle($numbers) to give yourself a randomly shuffled array of numbers Link to comment https://forums.phpfreaks.com/topic/48148-fatal-error-maximum-execution-time-of-30-seconds-exceeded/#findComment-959865 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.