This paper mainly introduces the difference between for loop and foreach in PHP, which has good reference value. Let's take a look at the little series.
The difference between A for loop and foreach
foreach relies on IEnumerable.
The first time var A in GetList () is called GetEnumerator returns the first object and assigns A,
The MoveNext is called after each subsequent execution of Var A in GetList (). Until the end of the loop.
The period getlist () method executes only once.
+ View Code
The For loop is positioned by subscript. LIST[3] equivalent to * (List + 3).
+ View Code
The or loop will call GetCount () to compare the length of each loop. While foreach does not consider the length, it only calls once GetList ().
Conclusion.
The For loop efficiency is higher than foreach when the fixed length or length does not require calculation.
It is convenient to use foreach in the uncertain length, or when the calculation length has a performance loss.
And the object in the collection is locked when foreach. The period cannot be modified.
The above is the PHP for loop and the difference between foreach detailed content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!