if语句不工作继续触发函数

I have a problem with my script im using. the if $addcount is not working. it continues to fire the insertMessage function when i dont want it to. Any ideas why?

if(($result->data != '') && ($result->data != null)) {
    $wallCount = 0;
    $addCount = 0;
    foreach($result->data as $thread){
        $wallCount++;
        $fromId = $thread->from->id;
        $message = $thread;
        if($fromId == $user['Id']){
            if($addCount < 100) {
                insertMessage($user['Id'], $message, $user['Num']);
                $addCount++;
                sleep(1);
            }
        }
     }
}

From your script, it will call insertMessage 100 times (0-99). Before the if statement, echo the counter to see what it is doing:

...
echo $addCount."<br />";
if($addCount < 100) {
...