Like iterrating a array, and using the index variable later instead of count() :
foreach($arr as $index => $val){
...
}
echo 'number of items: '.$index+1;
?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The language is explicitly OK with it.
I would not recommend it though, since it’s unusual to reuse loop-specific values outside the loop and you may introduce bugs if you refactor your loop some time later and forget the dependency a few lines down. In fact, it’s not a bad idea to explicitly
unset($index, $val)after the loop to avoid such problems. This is especially true if you loop by reference (foreach ($foo as &$bar)).