therealwesfoster Posted December 3, 2007 Share Posted December 3, 2007 I have a code that gets the longest (strlen) index from an array, and saves in to a variable: I get no errors, but the $length var stays at 0.. what am i missing lol? <?php // Get longest index $ckline = explode("; ",$ck); $length = 0; for ($i=0; $i<=count($ckline); $i++) { if ( $length < strlen($ckline[$i]) ) { $length = $ckline[$i]; } else { $length = $length; } } echo $length; ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted December 3, 2007 Share Posted December 3, 2007 Shouldn't you be setting length to strlen($ckline[$i]); <?php // Get longest index $ckline = explode("; ",$ck); $length = 0; foreach ($ckline as $ck) { $length = max($length, strlen($ck)); } echo $length; ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 3, 2007 Share Posted December 3, 2007 Your comparison is wrong. Try this: <?php $ckline = explode("; ",$ck); $length = 0; foreach ($ckline as $line) $length = (strlen($line) > $length)?strlen($line):$length; echo $length . '<br>'; ?> Ken Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted December 3, 2007 Author Share Posted December 3, 2007 Wow.. thats embarassing.. lol, thanks for pointing out the obvious Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.