PHP While Loop Array Input Tutorial
In PHP, you can use a while loop to iterate through an array and perform input operations. Here is an example:
<?php
$fruits = array("apple", "banana", "orange");
$i = 0;
while($i < count($fruits)) {
$fruit = $fruits[$i];
echo "Enter the quantity of {$fruit}: ";
$quantity = readline();
echo "You have entered {$quantity} {$fruit}(s)." . PHP_EOL;
$i++;
}
?>
In the example above, we first defined an array containing fruit names. We then used a while loop to iterate through each element in the array. Within the loop, we used the readline() function to receive the user’s input for the quantity of fruit and outputted it. Finally, we incremented the $i variable to move on to the next element in the array.