MBrody Posted November 21, 2006 Share Posted November 21, 2006 I am new to PHP. I am trying to do a quick edit to a page that was left behind by a developer that has gone MIA.Here is my question...I have a for statement that performs a loop:for($counter = 1; $counter < 10; $counter++) {print "Some text here";}I would like to know how to display a block of text if the loop parameters are not true, i.e. if $counter = 0. I would like to display:Alternate text here!Any help would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/27997-need-help-with-loop-statement/ Share on other sites More sharing options...
Orio Posted November 21, 2006 Share Posted November 21, 2006 I am having a hard time understanding you.The current loop displays "Some text here" 9 times. What do you want to do?Orio. Quote Link to comment https://forums.phpfreaks.com/topic/27997-need-help-with-loop-statement/#findComment-128063 Share on other sites More sharing options...
taith Posted November 21, 2006 Share Posted November 21, 2006 [code]<?if($counter=="0"){ echo '0 in counter';}else{ while($counter < 10){ echo "<br>Some text here"; $counter++; }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/27997-need-help-with-loop-statement/#findComment-128064 Share on other sites More sharing options...
wildteen88 Posted November 21, 2006 Share Posted November 21, 2006 You cant. As you are setting up the count variable in the for loop itself. The only time when you'll get a false result is when the value of the counter variable is not less than 10. When that happens the for loop stops looping. Quote Link to comment https://forums.phpfreaks.com/topic/27997-need-help-with-loop-statement/#findComment-128066 Share on other sites More sharing options...
wildteen88 Posted November 21, 2006 Share Posted November 21, 2006 You cant. As you are setting up the counter variable in the for loop itself. The only time when you'll get a false result is when the value of the counter variable is not less than 10. When that happens the for loop stops looping. Quote Link to comment https://forums.phpfreaks.com/topic/27997-need-help-with-loop-statement/#findComment-128067 Share on other sites More sharing options...
MBrody Posted November 21, 2006 Author Share Posted November 21, 2006 I appologize if I was not clear.Let me see if I can clarify.Basically, I would like the php code to check a parameter, if certain parameters are met, then a "looped" block of text would display. If the parameters are NOT met, then an alternate block of text would be met.So:If $counter was set to 1 -> a block of looped text would display.If $counter was set to 0 -> an alternate block of text would display.Does this make sense?Basically I know how to accomplish this basic principle with an "if" "else" statement. However, I need a loop statement because I am trying to display results. If the variable ($counter) is set to 0, I do not want results displayed but rather some alternate text messages.Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/27997-need-help-with-loop-statement/#findComment-128078 Share on other sites More sharing options...
Orio Posted November 21, 2006 Share Posted November 21, 2006 [code]<?phpif($counter == 1){ //run the loop- counter is 1 for($counter = 1; $counter < 10; $counter++) { print "Some text here"; }}elseif($counter == 0){ //do stuff if counter is 0}else{ //do stuff if counter is not 0 or 1}?>[/code]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/27997-need-help-with-loop-statement/#findComment-128081 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.