3

I've got several working while loops in my function but this one I just can't get to work as it's supposed to. I assumed that while($variable == false) would break out of the loop as soon as $variable = true; occurs. Here's a bare bones version, with only two statements inside the loop.

$excluded = false;
while($excluded == false):
    $excluded = in_array(strtolower($extension), $GLOBALS['codexts']) ? true : false;
    $excluded = $file != "." && $file != ".." ? false : true; 
    break;
endwhile;

Inside the loop, the first definition does produce false when it's suppose to, but the loop doesn't break. It goes on to the second one, which returns true. If I change the second one to this:

$excluded = $excluded ? $excluded : ($file != "." && $file != ".." ? false : true); 

...then I get the result I want, but only because I'm correcting for the failure of the while loop to break when $excluded is first defined as true. What am I doing wrongly here?

UPDATE I just reread the manual, and apparently I missed this part:

The value of the expression is checked each time at the beginning of the loop, so even if this value changes during the execution of the nested statement(s), execution will not stop until the end of the iteration

That's why the iterations were reverting back to false. I do need to do manual conditional breaks after all.

Works for a Living
  • 1,262
  • 2
  • 19
  • 44
  • Are you sure it goes on to the second loop? Your `break;` statement does not have a condition surrounding it, it will be executed whether or not your `$excluded` is true or false. – AyB Apr 30 '14 at 05:35
  • Missing curly brackets ..... – Avinash Babu Apr 30 '14 at 05:36
  • The break statement is at the end of the loop, to cover those iterations that never return true, so the loop doesn't get stuck. But I assure you, there are iterations that are returning true on the first statement, but not behaving like they're true, hence that's how I know it's going on to the second statement. The break at the end is not the issue. – Works for a Living Apr 30 '14 at 05:37
  • No, missing curly brackets is not the issue. – Works for a Living Apr 30 '14 at 05:46

2 Answers2

3

try

$excluded = false;
while($excluded == false) {
   $excluded = in_array(strtolower($extension), $GLOBALS['codexts']) ? true : false;
   if ($excluded == true) {
     break;
   }
    $excluded = $file != "." && $file != ".." ? false : true; 
    break;
} 
?>
Sajad Karuthedath
  • 14,987
  • 4
  • 32
  • 49
0

Q: Why don't use use conventional syntax?

Q: Why a redundant "break"?

Q: Why are you setting the expression twice before testing, and do you really need "?:" here? Twice???

Q: Can't you simplify the expression that changes your flag (changes "$excluded")?

SUGGESTION:

$myvar = false;
while(!$myvar) {
  $myvar = << something that sets/clears my flag >>
}
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
  • Q: Which part of my syntax is unconventional? A: The break statement is at the end of the loop, to cover those iterations that never return true, so the loop doesn't get stuck. It's standard protocol. It is NOT redundant. Q: I don't understand your third question: "setting the expression twice before testing." – Works for a Living Apr 30 '14 at 05:39
  • It doesn't get much simpler than `if in_array() true/false`. – Works for a Living Apr 30 '14 at 05:41
  • And your suggestion is exactly what I'm doing already. `!$myvar` is the same as `$myvar == false`, and I tried `!$excluded` many times before posting here. – Works for a Living Apr 30 '14 at 05:45
  • use the wrapped value of $myvar for your while `while !$myvar.wrappedValue {` – theZ3r0CooL Feb 26 '22 at 01:57