dazman Posted June 3, 2009 Share Posted June 3, 2009 I know the answer is just me, but I am new to this. I am trying to run the following script but i am having all sorts of problem output. At the moment the script is just generating the number 1. I am not familiar with php code, and i have read the manual on while loops. I dont have a debugger, could you suggest a good one for me perhaps, if anyone could let me know what I have done wrong here it would be greatly appreciated. <?php /* This is a script that is supposed to generate random numbers from 1-45. Each time it generates a new random number the count of the randomly generated numbers is noted. If a random number that is generated is equal to 44, 10, 16, 12, 22 or 20 the script adds to a counter. Once the counter reaches 3 the script will end and the noted amount of random numbers the script generated it output. */ set_time_limit(500); $counter = 0; $randomnumbercounter = 0; while($counter < 3) { $random = rand(1,45); if ($random = 44) { $counter++; } if ($random = 10) { $counter++; } if ($random = 16) { $counter++; } if ($random = 12) { $counter++; } if ($random = 22) { $counter++; } if ($random = 20) { $counter++; } $randomnumbercounter++; } echo '<BR>'; echo $randomnumbercounter; ?> Link to comment https://forums.phpfreaks.com/topic/160778-solved-while-loop-not-working-properly/ Share on other sites More sharing options...
gevans Posted June 3, 2009 Share Posted June 3, 2009 Your problem is with the if statements; if ($random = 44) { With only a single equals sign your assigning a value to $random (which returns boolean true). You need two (or three) equals signs; if ($random == 44) { Link to comment https://forums.phpfreaks.com/topic/160778-solved-while-loop-not-working-properly/#findComment-848529 Share on other sites More sharing options...
dazman Posted June 3, 2009 Author Share Posted June 3, 2009 thanks gevans, for your quick response. I knew it was something very simple. yes I was setting the variables rather than comparing. I need to pay more attention to detail. Is there any debugger you suggest I can use for future work? Link to comment https://forums.phpfreaks.com/topic/160778-solved-while-loop-not-working-properly/#findComment-848534 Share on other sites More sharing options...
gevans Posted June 3, 2009 Share Posted June 3, 2009 To be honest I've never looked for, or know if a 'debugger' as such exists. If you use a text editor it will show you syntax errors. Though it wouldn't have shown anything in your case as you weren't doing anything wrong as far as php is concerned, just different to what you wanted. Take a look at notepad++ http://notepad-plus.sourceforge.net/uk/site.htm It's a free source code editor Link to comment https://forums.phpfreaks.com/topic/160778-solved-while-loop-not-working-properly/#findComment-848536 Share on other sites More sharing options...
dazman Posted June 3, 2009 Author Share Posted June 3, 2009 Thanks, Notepad++ is great. Has so many languages . I can use it on my ubuntu machine aswell Link to comment https://forums.phpfreaks.com/topic/160778-solved-while-loop-not-working-properly/#findComment-848886 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.