From what I see in your original code, you say if(!%times == 8)
From what I can tell, you meant $times, not %times.
Also, I would say if($times != 8)
rather than if(!$times == 8)
I'm not sure how much of a difference that would make, if any, but it's easier to read and understand.
EDIT: Also, instead of saying $times = $times +1;
it makes more sense to use the simple $times++;