Jump to content

Need Help with Loop Statement


MBrody

Recommended Posts

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!
Link to comment
https://forums.phpfreaks.com/topic/27997-need-help-with-loop-statement/
Share on other sites

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.
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.
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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.