Jump to content

[SOLVED] Why doesnt this work?


therealwesfoster

Recommended Posts

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;
?>

Link to comment
https://forums.phpfreaks.com/topic/80006-solved-why-doesnt-this-work/
Share on other sites

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;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.