When writing code, we often want the same block of code to repeat a certain number of times. Instead of manually
duplicating lines in a script, we use loops. Loops execute a block of code as long as a specified condition is true.
We will explore the most common loops alongside the break and continue statements, which
help us precisely control their execution. Each loop has its own advantages and is chosen based on the specific
situation.
Depending on whether we know the number of repetitions in advance or depend on a condition, we choose the appropriate tool:
When working with while and do-while loops, you must always ensure that the condition
inside the loop will eventually evaluate to false. If you forget to change the value (for instance,
forgetting to increment the counter with $i++), the loop will never stop. This is called an
infinite loop, which can heavily overload the server or freeze the web browser!