PHP 在循环中鼓励循环

发布于 2024-11-07 22:13:28 字数 441 浏览 0 评论 0原文

我想要一个循环来循环,而不需要遍历循环的所有代码。例如,在示例代码中,我想继续循环,而不必遍历匹配语句下面的任何 if 语句。

注意:我知道使用 switch 语句可以更好地完成与此示例类似的操作,但我的 if 语句集并不像这样简单。

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;}
}

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;}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鯉魚旗 2024-11-14 22:13:28

使用继续

在循环结构中使用 continue 来跳过当前循环迭代的其余部分,并在条件评估时继续执行,然后在下一次迭代开始时继续执行。

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.

纵山崖 2024-11-14 22:13:28
foreach($x as $y){
    if($y==0){continue;}
    if($y==1){continue;}
    if($y==2){continue;}
    if($y==3){continue;} }

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

?

岁月无声 2024-11-14 22:13:28

在这里没有看到不使用 switch 的理由。

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

如果 switch 语句是 foreach 循环中唯一的

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

更新(在更新问题之后) ,您可以完全省略 continue; ):

switch 语句仍然可用(并且在我看来更具可读性)

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

,只要代码不执行任何操作,您也可以稍微简化它。

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

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
  }
}
橙幽之幻 2024-11-14 22:13:28

你的意思是这样吗?

foreach($x as $y) {

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

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

Do you mean like this?

foreach($x as $y) {

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

    // or something like this?
    switch($y) {
          case 2:
              doThat();
              continue;
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文