views:

48

answers:

4

I'm stumped on this and my searches aren't turning up anything relevant.. I need to do a while loop that will continue if either of 2 variables are true... as far as I can tell you can't do a "while ($var = '' and $var2 = ''); so I tried this, basically I figured I could just set 2 different if statements so that it would change the variable "continue" if it went past 4 iterations (if $i >= 4), however this just gives an infinite loop:

function whiletest () {
    $i = 1;
    do {
        echo 'output';
        if ($status != 'true') {
            $continue = 1 ;
        } 
        if  ($i >= 4) {
            $continue = 2 ;
        }
        $i++ ;
    } while ($continue = 1 );
} 
+1  A: 

The while statement evaluates a boolean expression. You should be able to write out:

while( ($status != true) && ($continue == 1) ) {}

Also in your code (if its a c/p), you have $continue = 1. This will always evaluate to true.

EDIT:

while (($status) && ($i < 4))

As for the last while, it just looks like an infinite loop to me.

Joel Etherton
What I want is for it to stop the loop if $status != 'true' (status is not equal to 'true') or to stop if $i is greater or equal to '4', I guess I'm not understanding why my second if with "$continue = 2;" is not sufficient to change the continue value to make the loop stop.
Rick
@Rick - Ok let me throw in an edit.
Joel Etherton
+2  A: 

Are you looking for a construct like this:

while($var1 == 'value1' OR $var2 == 'value2') {
    ...
}

That will continue to run while either condition is true.

Kalium
+2  A: 

Why wouldn't the following work?

while (($condition1) || ($condition2)) {
    // loop stuff
}

As long as the expression within the while parens is true, the loop will execute.

Andy
Using this model you can also make flag variables/methods with boolean returns and determine their evaluation from separate parts of the script.
DeaconDesperado
A: 

You shouldn't need the $continue variable. This should do the trick:

$i = 1;
do {
    //do other stuff here (possibly changing the value of $status)
    echo 'output';
    $i++;
} while ($status != 'true' && $i < 4);

Keep in mind that this will always run the loop at least once. If $status might start out as 'true' and you want the loop to run zero times if it is, you want:

$i = 1;
while ($status != 'true' && $i < 4) {
    //do other stuff here (possibly changing the value of $status)
    echo 'output';
    $i++;
}
NChase