i want check string length of comment
array. once of them equal or higher 4, want echo relevant value, , stop.
i guessed using while
should good, if break loop @ 4
or more, nothing gets echoed. if break @ 5
or more, previous 2 4-string values echoed, want first 4-string value echoed, , stop.
$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 echoed // if (strlen((string)$comment[$x]) >= 5) break; // 2 values echoed }
also, there maybe better/shorter practice check thing, maybe built in function in_array
?
the problem code loop body checks/prints 1 element , breaks on different 1 because increment pointer between 2 points. have moved break statement above increment, or put if statement (much @a-2-a suggested). should work expected.
with break above increment:
while ($x <= 10) { if (strlen((string)$comment[$x]) >= 4 ) { echo $comment[$x]; echo "<br/>"; } if (strlen((string)$comment[$x]) >= 4) break; $x = $x + 1; }
with combined echo/break:
while ($x <= 10) { if (strlen((string)$comment[$x]) >= 4 ) { echo $comment[$x]; echo "<br/>"; break; } $x = $x + 1; }
also may want iterate array it's length instead of hardcoded limit of 10:
$x = 0; $length = count($comment); while ($x < $length) { // ... }
Comments
Post a Comment