for loop only works first time function is called

I am trying to echo the following function:
[php]
function LikeCounter($names)
{
$likes = 0;
$count = strlen($names);
echo($count . “
”);
echo($names);
for ($i = 0; $i == $count; $i++)
{
echo(‘Works!’);
if ($names{$i} == ‘,’)
{
$likes += 1;
}
}
return $likes;
}
[/php]

$names is a string containing usernames separated by a comma.

When I register a new user to my website their profile is created on the database with a “Welcome!” comment.

When I go to their profile, the comment is displayed and above this comment, “0” is echoed since $names = “”. After this is the message “Works!” is displayed and since there are no commas in $name, the returned value is 0. Everything seems to be working fine.

When I “like” this comment, $names now contains the value "username, " and if I like it again it will be username, username, " etc.

The problem is that after I’ve “liked” a comment, the message “Works!” is no longer displayed and the returned value is still 0. It seems like the function ignores the for loop every time I call the function except for the first time.

When new comments are added to a profile, the same thing happens; the for loop only works the first time when $names = “”.

Any ideas?

Thanks in advance :slight_smile:

Fixed it!

For some reason, I was thinking that the $i==$count meant it will loop until this is true rather than as long as it it true.

Fail.

Sponsor our Newsletter | Privacy Policy | Terms of Service