Possible Duplicate:
for vs foreach vs while which is faster for iterating through arrays in php
For each and while, which of these is faster, recommended, and what are their resource usages like?
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.
foreachis optimized for iteration over collections. That’s a fancy way of saying that it works best (and fastest) when you use it with arrays (and as of PHP5, objects). You can usewhile()to get the same effect as a foreach, but its not as efficient since you need to calllist()inside thewhileloop.foreach($array as $key => $value)written with awhileiswhile(list($key,$value) = each($array)). You can see the number of extra calls that are needed.