conditional while loop in php?

Posted by julio on Stack Overflow See other posts from Stack Overflow or by julio
Published on 2010-03-12T19:58:04Z Indexed on 2010/03/12 20:07 UTC
Read the original article Hit count: 189

Filed under:
|

I'm pretty sure there's an obvious answer for this-- hoping someone can help me--

I need to do a PHP while loop, but only if a variable is true. And I can't really put the while loop in an "if" statement, which seems like the obvious thing to do, since the code block is huge and it would be ugly and confusing. Do I need to break out the code in the loop into a function, or is there an easier way to deal with this?

Here's the basic idea:

if(condition){
  while(another_condition){
    //huge block of code loops many times
  }
} else {
  // huge block of code runs once
}

I want the huge block of code to execute regardless of the state of the condition variable-- but only to execute once if condition is false, and execute for as long as another_condition is true if condition is true.

Hope that's clear!

The following code doesn't work, but gives an idea of what I want to accomplish:

if(condition){ while(another_condition){ }
  // huge block of code
if (condition){ } } // closes the while loop-- obviously throws an error though!

thanks in advance.

© Stack Overflow or respective owner

Related posts about php

Related posts about looping