Home >Backend Development >PHP Problem >What is the difference between for and foreach in php
Difference: 1. for needs to know the length of the array before operating, but foreach does not; 2. foreach is much more efficient than for; 3. foreach directly obtains the next value through the next pointer in the structure, while for loop It needs to be hashed first according to the key to get the value.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
The difference between for loop and foreach in php As follows:
1. for needs to know the length of the array before operating. Foreach does not need to
2. Foreach is much more efficient than for. The main reason is that for needs to perform many conditional judgments
3. From the perspective of the data structure of the array variable,
foreach directly obtains the next value through the next pointer in the structure, while the for loop needs to hash first according to the key to obtain the value.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the difference between for and foreach in php. For more information, please follow other related articles on the PHP Chinese website!