PHP Interview Questions
What is the difference between for and Foreach loop in PHP?
For loop is mainly used for iterating a pre-defined number of times and Foreach loop is used for reading array elements or MySQL result set where the number of iteration can be unknown.
Example For Loop :
//Loop will iterate for 5 times
for ($n = 0; $n <= 5; $n++) {
echo "The number is: $n <br>";
Example Foreach :
//Loop will iterate based on array elements 
$parts = array("HDD", "Monitor", "Mouse", "Keyboard"); 
foreach ($parts as $value) { 
 echo "$value <br>";