1

I am looking for an element in multiple while loops, but if the element is found, I don't need to check the following while loops. How can I achieve something like this?

while(something) {
    if (this = key) {
        // break this loop and skip the next ones. Otherwise proceed to next. 
    }
}

while(somethingelse) {
    if (this = key) {
        // break this loop and skip the next ones. Otherwise proceed to next. 
    }
}

while(somethingthird) {
    if (this = key) {
        // break this loop and skip the next ones. Otherwise stop looking. 
    }
}
Mistalis
  • 17,793
  • 13
  • 73
  • 97
Rhbphp
  • 31
  • 4

5 Answers5

3

Use a variable (boolean) that knows if your condition was checked in the previous while, like this:

$breakAll = false; //default false
while(something) {
    if (this = key) {
        // break this loop and skip the next ones. Otherwise proceed to next. 
        $breakAll = true; //now break all whiles
    }
}

while(somethingelse && !breakAll) {
    if (this = key) {
        // break this loop and skip the next ones. Otherwise proceed to next. 
        $breakAll = true; //same here
    }
}

while(somethingthird && !breakAll) {
    if (this = key) {
        // break this loop and skip the next ones. Otherwise stop looking. 
    }
}
Mistalis
  • 17,793
  • 13
  • 73
  • 97
2

Refactor the while loops to a function and return when the element is found

function doSomething() {
    while(something) {
        if (this == key) {
            return;
        }
    }

    while(somethingelse) {
        if (this == key) {
            return;
        }
    }

    while(somethingthird) {
        if (this == key) {
            return;
        }
    }
}
napuzba
  • 6,033
  • 3
  • 21
  • 32
0

Use a function

function findSpecificElement($element)
{
    while ($something) {
       if ($something == $element) {
         return $something;
       }
    } 

    while ($something) {
       if ($something == $element) {
         return $something;
       }
    } 

    ...
}
Depzor
  • 1,126
  • 1
  • 12
  • 21
0

More correct to use php operator: continue, see:

http://php.net/manual/en/control-structures.continue.php

Example:

while (list($key, $value) = each($arr)) {
    if (!($key % 2)) { // skip even members
        continue;
    }
    do_something_odd($value);
}

Operator: break; - break the loop http://php.net/manual/en/control-structures.break.php

while(something) {
    if (this == key) {
       break;
    }
}
CETb
  • 344
  • 1
  • 3
  • 11
-1

goto can be used in a situation like this to jump over the while loops that should not be run.

while(something) {
    if (this = key) {
        goto yourDoom; // Breaks out of loop and goes to label "yourDoom"
    }
}
while(somethingelse) {
    if (this = key) {
        goto yourDoom; // Breaks out of loop and goes to label "yourDoom"
    }
}
while(somethingthird) {
    if (this = key) {
        goto yourDoom; // Breaks out of loop and goes to label "yourDoom"
    }
}
yourDoom: // This is a label that you can go to
// Do something
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
  • 1
    I would never recommend using goto as it results in hard to follow code. http://stackoverflow.com/questions/1900017/is-goto-in-php-evil – grepsedawk Sep 14 '16 at 12:25
  • @Pachonk This is not even close to being hard to follow. `goto` in PHP does not have the same problems as in other languages, such as C, where using it can cause serious problems. It should have been named something different so people don't mistake the two for the same thing. – Sverri M. Olsen Sep 14 '16 at 13:22