What is the purpose of using a while loop in PHP?

Using a while loop in PHP allows you to repeatedly execute a block of code as long as a specified condition is true. This is useful when you need to perform a task multiple times without knowing in advance how many iterations are required. It provides a flexible way to handle situations where the number of iterations is not fixed.

// Example of using a while loop to print numbers from 1 to 5
$counter = 1;
while ($counter <= 5) {
    echo $counter . "<br>";
    $counter++;
}