Staying in the Loop

Loops in PHP let you run the same piece of code repeatedly while some condition is still true. There are four main kinds. while keeps looping as long as its condition stays true. do…while is similar, but it always runs the code once before checking the condition. for is the one with the classic three expressions — setup, condition, and increment — perfect when you know exactly how many times to loop. And foreach is the friendliest for arrays, automatically looping over each element. The next lessons dive into each one with examples.