【发布时间】:2017-02-02 02:48:15
【问题描述】:
我有不同的页面,每个页面都包含一些数组,
例如:
第 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 个)
我想应用一些名为 small 和 big 的样式类,或者根本没有类,这取决于两个条件:
默认不设置任何类。
如果数组中存在图像,请检查前 15 个数组的字符串长度。 如果它们都短于 11 个字符,则设置一个
big类。否则,设置一个small类。无论图像是否存在,如果前 15 个数组中至少有一个数组包含 14 个或更多字符,则设置一个
small类(如果已设置则覆盖big类早)
这是代码,我不确定何时以及如何使用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?
}
【问题讨论】:
标签: php loops for-loop conditional break