0

I am wanting to get a loop to loop without going through all the code for the loop. For instance in the example code I want to continue the loop without have to go through any of the if statements below the one that matches.

NOTE: I know something similar to this example would be better done using a switch statement but my set of if statements are not as simple as this.

foreach($array as $key=>$value){
    if(is_varchar($value)){loop;}
    if(is_text($value)){loop;}
    if(is_mediumText($value)){loop;}
    if(is_boolean($value)){loop;}
    if(is_integer($value)){loop;}
    if(is_float($value)){loop;}
    if(is_double($value)){loop;}
}
2
  • Showing us the actual statements would be better. Commented May 17, 2011 at 12:19
  • add a break statement in the condition that matches? Commented May 17, 2011 at 12:20

4 Answers 4

3

Use continue

continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.

Sign up to request clarification or add additional context in comments.

Comments

3
foreach($x as $y){
    if($y==0){continue;}
    if($y==1){continue;}
    if($y==2){continue;}
    if($y==3){continue;} }

?

Comments

1

Dont see a reason NOT to use switch here.

foreach ($x as $y) {
  switch ($y) {
    case 0: continue; break;
    case 1: continue; break;
    case 2: continue; break;
    case 3: continue; break;
  }
}

You can completely omit continue;, if the switch-statement is the only one within the foreach-loop

foreach ($x as $y) {
  switch ($y) {
    case 0: doSomethingA(); break;
    case 1: doSomethingB(); break;
    case 2: doSomethingC(); break;
    case 3: doSomethingD(); break;
  }
}

Update (after the update of the question):

The switch-statement is still useable (and in my opinion more readable)

foreach($array as $key=>$value){
  switch (true) {
    case is_varchar($value): continue; break;
    case is_text($value): continue; break;
    // And so on
  }
}

As long as the code simply does nothing you can also simplify it a little bit.

foreach($array as $key=>$value){
  switch (true) {
    case is_varchar($value): 
    case is_text($value):
      continue;
    break;
    // And so on
  }
}

3 Comments

If you see the note I said that a switch statement does not work if the set of if statements I am using. I have changed the example code to reflect this.
@Brook: Didnt understand it the first time, but anyway: Dont see a problem :)
I didn't know you could use functions in a switch stament like that. I like it!
0

Do you mean like this?

foreach($x as $y) {

    if ($y == 1) {
        doThis();
        continue;
    }

    // or something like this?
    switch($y) {
          case 2:
              doThat();
              continue;
     }
}

1 Comment

I can't use a switch statement so it would be more like the your first example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.