What is the difference between a for loop and a while loop in PHP, and when should each be used?

A for loop is used when you know the number of iterations you want to perform, while a while loop is used when you want to continue iterating until a certain condition is met. For loops are typically used when you know the exact number of times you want to loop through a block of code, while while loops are used when you want to keep looping until a specific condition is no longer true. For example, if you want to loop through an array with a known number of elements, you would use a for loop. On the other hand, if you want to keep looping until a user input is received, you would use a while loop.

// Using a for loop to iterate through an array
$colors = ["red", "green", "blue"];
for ($i = 0; $i < count($colors); $i++) {
    echo $colors[$i] . "<br>";
}

// Using a while loop to keep looping until a user input is received
$userInput = "";
while ($userInput !== "quit") {
    $userInput = readline("Enter a value (type 'quit' to exit): ");
    echo "You entered: " . $userInput . "<br>";
}