何时使用' Break'在For循环中?

时间:2016-09-24 08:43:20

标签: php loops for-loop conditional break

我有各种页面,每个页面都包含一些数组,

例如:

第1页只包含2个数组:

$textual_button[1] = "I am a long character";
$textual_button[2] = "I am also a long character";

第2页包含20个数组:

$textual_button[1] = "I am a long character";
...
...
$textual_button[19] = "I am also a long character";
$textual_button[20] = "I am also a very long character";

因此,我想检查前15个数组中的某个条件(可能存在 - 如第2页中所示,或者可能不存在 - 如第1页所示,仅包含2个数组)

我想应用一些名为smallbig的样式类,或者根本不应用任何类,具体取决于两个条件:

  1. 默认情况下,不要设置任何类。

  2. 如果数组中存在图像,请检查前15个数组的字符串长度。 如果它们都短于11个字符,请设置big类。否则,设置small类。

  3. 无论图像是否存在,如果前15个中至少有一个数组有14个或更多字符,则设置small类(并覆盖big类如果它是先前设定的那样)

  4. 这是代码,我不确定何时以及如何使用break

    $class = "nothing"; //default class
    
    if (in_array(true, $image)) { // If there's any image in array :
    
        $class = "small"; // by default, apply small class 
    
            for($x = 1;$x <= 15;$x++){  // check on array 1 to 15
                if (strlen((string)$textual_button[$x]) <= 11) {  // if all textual string are  less or equal to 11 characters , make them big
                $class = "big";  
                };
            break; // Is this necessary?
            } 
    }
    
     // In general, no matter if there's an image in array:
    
    for($x = 1;$x <= 15;$x++){  // check on array 1 to 15
        if (strlen((string)$textual_button[$x]) >= 14) {  // if even one textual string is 14 or more, make all of them small.
          $class = "small";
        };
        break; // When is it necessary?
    }
    

4 个答案:

答案 0 :(得分:1)

(defmacro make-my-function (name) (list 'defun (intern (format "my-%s-function" name)) () (list 'interactive) '(message "It's work !"))) 突破循环,所以在这两种情况下,你的循环只运行一次,或者第一次迭代,然后停止。

所以不,你不应该在这里使用break,因为你已经将迭代次数限制在15个。

break可能有用(还有其他方法......)如果你循环遍历数组中的所有元素,保留一个计数器,然后检查计数器是否为15打破了循环。

请注意,你可能想要一个break循环而不是foreach循环作为for循环,当你的元素少于15个时,15次迭代将导致php警告阵列。

在您的情况下,我个人会做的是将数组切割为最多15个元素(当少于......时减少)和for上的元素。

最后一件事:你在循环中覆盖你的foreach变量,所以它可能不会按照你的预期去做。如果任何元素不符合您的条件,您应该将布尔值设置为$class,然后在循环之后根据布尔设置类。

一般来说,如果任何需要匹配,你可以使用break,但如果 all 需要匹配,你总是需要完成循环。

答案 1 :(得分:1)

也许试试这个

$class = "nothing"; //default class

if (in_array(true, $image)) { // If there's any image in array :

    $class = "small"; // by default, apply small class 

        for($x = 1;$x <= 15;$x++){  // check on array 1 to 15
            if (strlen((string)$textual_button[$x]) <= 11) {  // if all textual string are  less or equal to 11 characters , make them big
            $class = "big";
            };
            else {
            $class = "small";
            break;
            }
        } 
}

 // In general, no matter if there's an image in array:

for($x = 1;$x <= 15;$x++){  // check on array 1 to 15
    if (strlen((string)$textual_button[$x]) >= 14) {  // if even one textual string is 14 or more, make all of them small.
      $class = "small";
      break;
    };
}

答案 2 :(得分:0)

  

你能说出这样的意思吗?

<?php

    $class      = "nothing"; 
    if (in_array(true, $image)) {    
        $class  = "small"; 
        for($x = 1;$x <= 15;$x++){  
            if (strlen( (string)$textual_button[$x] ) <= 11) { 
                $class = "big";
                // BREAK OUT OF THE LOOP INSIDE THE IF CLAUSE SINCE YOU HAVE REACHED YOUR GOAL
                // WHICH IS : THE LENGTH OF $textual_button[$x] IS LESS THAN OR EQUAL TO 11  
                break;
            }else{
                // NO NEED FOR AN EXTRA LOOP SINCE IT IS ESSENTIALLY THE SAME LOOP
                // JUST DO WHATEVER YOU WISH WITHIN THE ELSE CLAUSE...
                // THAT IS IF: THE LENGTH OF $textual_button[$x] IS GREATER THAN OR EQUAL TO 14  
                // AGAIN BREAK OUT OF THE LOOP INSIDE THE ELSE CLAUSE SINCE YOU HAVE REACHED YOUR GOAL
                $class = "small";   
                break;             
            }             
        }
    }
  

还是这样的?

    $class      = "nothing";
    if (in_array(true, $image)) {
        $class  = "small";
        for($x = 1;$x <= 15;$x++){
            if (strlen( (string)$textual_button[$x] ) <= 11) {
                $class = "big";
                // BREAK OUT OF THE LOOP INSIDE THE IF CLAUSE SINCE YOU HAVE REACHED YOUR GOAL
                // WHICH IS : THE LENGTH OF $textual_button[$x] IS LESS THAN OR EQUAL TO 11
                break;
            }
        }
    }

    for($x = 1;$x <= 15;$x++){  // check on array 1 to 15
        if (strlen( (string)$textual_button[$x]) >= 14 ) {
            // AGAIN BREAK OUT OF THE LOOP INSIDE THE ELSE CLAUSE SINCE YOU HAVE REACHED YOUR GOAL
            // THAT IS IF: THE LENGTH OF $textual_button[$x] IS GREATER THAN OR EQUAL TO 14
            $class = "small";
            break;
        };
    }

答案 3 :(得分:0)

在您的两种情况下,只会发生一次for循环,因为您在不检查任何内容的情况下打破它。如果要检查所有数组元素是否符合条件,则应将break置于if条件内:

for ($x = 1; $x <= 15; $x++) {
    if (strlen((string)$textual_button[$x]) <= 11) {
        $class = "big";
        break;  
    };
 }

在这种情况下,如果数组中的任何元素短于11个符号,$class将变为bigfor循环将停止执行。如果您更改代码与第一个示例类似,则第二个for也是如此。