guyfromfl Posted August 4, 2011 Share Posted August 4, 2011 I am trying to create a loop that increments by .5 This just runs forever: for ($bathrooms=0; $bathrooms<=8; $bathrooms = $bathrooms + .5) { echo "<option value=\"$bathrooms\">$bathrooms</option>\n"; } Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/243838-brain-fart-with-for-loops/ Share on other sites More sharing options...
AyKay47 Posted August 4, 2011 Share Posted August 4, 2011 for ($bathrooms=0; $bathrooms<=8; $bathrooms += .5) { echo "<option value=\"$bathrooms\">$bathrooms</option>\n";} Quote Link to comment https://forums.phpfreaks.com/topic/243838-brain-fart-with-for-loops/#findComment-1251988 Share on other sites More sharing options...
AbraCadaver Posted August 4, 2011 Share Posted August 4, 2011 No, that is perfectly fine and will do what you want. Quote Link to comment https://forums.phpfreaks.com/topic/243838-brain-fart-with-for-loops/#findComment-1251989 Share on other sites More sharing options...
guyfromfl Posted August 4, 2011 Author Share Posted August 4, 2011 hmmm it just runs and times out... There must be another issue Quote Link to comment https://forums.phpfreaks.com/topic/243838-brain-fart-with-for-loops/#findComment-1251991 Share on other sites More sharing options...
cunoodle2 Posted August 4, 2011 Share Posted August 4, 2011 AyKay47 had the correct answer. What output, if any are you getting? Can you put it on it's OWN page as I am betting that you have other script on the page that is causing issues. Quote Link to comment https://forums.phpfreaks.com/topic/243838-brain-fart-with-for-loops/#findComment-1251994 Share on other sites More sharing options...
PFMaBiSmAd Posted August 4, 2011 Share Posted August 4, 2011 There's nothing wrong with your posted code. Your actual code must be modifying the $bathrooms variable inside of the loop. Quote Link to comment https://forums.phpfreaks.com/topic/243838-brain-fart-with-for-loops/#findComment-1251995 Share on other sites More sharing options...
AyKay47 Posted August 4, 2011 Share Posted August 4, 2011 really what I wrote is just a neater way of writing what the OP had, the way that you wrote it will not cause a timeout...that is normally caused by an infinite loop or something along the sorts...are any errors reported here? edit: also, the newline character that you have in your code "\n" is irrelevant if this is to be parsed as HTML, use a break "<br />" instead Quote Link to comment https://forums.phpfreaks.com/topic/243838-brain-fart-with-for-loops/#findComment-1251996 Share on other sites More sharing options...
guyfromfl Posted August 4, 2011 Author Share Posted August 4, 2011 I commented out the loop and its still running forever... There has to be an issue in the script or with the server...gonna restart and comment out all the php until I find the problem. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/243838-brain-fart-with-for-loops/#findComment-1251997 Share on other sites More sharing options...
AyKay47 Posted August 4, 2011 Share Posted August 4, 2011 if you cannot solve this, post the code and we will help you with this issue.. Quote Link to comment https://forums.phpfreaks.com/topic/243838-brain-fart-with-for-loops/#findComment-1251999 Share on other sites More sharing options...
guyfromfl Posted August 4, 2011 Author Share Posted August 4, 2011 Thanks AyKay, I restarted the server and is running fine.. BTW.. I went with your code convention Quote Link to comment https://forums.phpfreaks.com/topic/243838-brain-fart-with-for-loops/#findComment-1252002 Share on other sites More sharing options...
AyKay47 Posted August 4, 2011 Share Posted August 4, 2011 Thanks AyKay, I restarted the server and is running fine.. BTW.. I went with your code convention Quote Link to comment https://forums.phpfreaks.com/topic/243838-brain-fart-with-for-loops/#findComment-1252007 Share on other sites More sharing options...
Pikachu2000 Posted August 4, 2011 Share Posted August 4, 2011 edit: also, the newline character that you have in your code "\n" is irrelevant if this is to be parsed as HTML, use a break "<br />" instead The \n is not irrelevant. It causes a newline in the underlying markup itself. Quote Link to comment https://forums.phpfreaks.com/topic/243838-brain-fart-with-for-loops/#findComment-1252194 Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 I don't think the code is problem, maybe its because you have too many bathrooms in your house... Quote Link to comment https://forums.phpfreaks.com/topic/243838-brain-fart-with-for-loops/#findComment-1252319 Share on other sites More sharing options...
AyKay47 Posted August 5, 2011 Share Posted August 5, 2011 edit: also, the newline character that you have in your code "\n" is irrelevant if this is to be parsed as HTML, use a break "<br />" instead The \n is not irrelevant. It causes a newline in the underlying markup itself. i know what it does, it's not needed..but the code is fine with it there as well, was my point Quote Link to comment https://forums.phpfreaks.com/topic/243838-brain-fart-with-for-loops/#findComment-1252474 Share on other sites More sharing options...
Pikachu2000 Posted August 5, 2011 Share Posted August 5, 2011 Then why suggest using a <br> instead? Using \n is the correct way to do what the OP did. Quote Link to comment https://forums.phpfreaks.com/topic/243838-brain-fart-with-for-loops/#findComment-1252539 Share on other sites More sharing options...
AyKay47 Posted August 5, 2011 Share Posted August 5, 2011 if the OP is looking to create a new line and is viewing this as HTML in the browser, which i presume he is, chances are the newline character "\n" will not give him a line break... which is why i suggested using the html break tag.. Quote Link to comment https://forums.phpfreaks.com/topic/243838-brain-fart-with-for-loops/#findComment-1252552 Share on other sites More sharing options...
Pikachu2000 Posted August 5, 2011 Share Posted August 5, 2011 You might want to read the code in the OP again. The \n is being properly used after the <option></option> tags in a <select> field. Quote Link to comment https://forums.phpfreaks.com/topic/243838-brain-fart-with-for-loops/#findComment-1252566 Share on other sites More sharing options...
AyKay47 Posted August 5, 2011 Share Posted August 5, 2011 you got me on this one pikachu, i forgot that it was in a select tag.. Quote Link to comment https://forums.phpfreaks.com/topic/243838-brain-fart-with-for-loops/#findComment-1252568 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.