【发布时间】:2016-04-30 22:32:18
【问题描述】:
我想检查comment 数组的字符串长度。
一旦其中任何一个等于或大于 4,我想回显相关值,然后停止。
我猜用while应该不错,
但如果我在4 或更多处打破循环,则没有任何回应。
如果我在5 或更多处打破它,前两个 4 字符串值将被回显,但我只希望第一个 4 字符串值得到回显,然后停止。
$comment[1] = "abc"; // add comment below text button
$comment[2] = "xyz"; // add comment below text button
$comment[3] = "abcd"; // add comment below text button
$comment[4] = "xyza"; // add comment below text button
$comment[5] = "abcde"; // add comment below text button
$comment[6] = "xyzab"; // add comment below text button
$x = 1;
while ($x <= 10) {
if (strlen((string)$comment[$x]) >= 4 ) {
echo $comment[$x];
echo "<br/>";
}
$x = $x + 1;
if (strlen((string)$comment[$x]) >= 4) break; // Nothing get echoed
// if (strlen((string)$comment[$x]) >= 5) break; // two values get echoed
}
另外,是否有更好/更短的做法来检查这个东西,也许是一些内置函数,比如in_array?
【问题讨论】:
标签: php loops while-loop break