Loop it up: Unleash the power of for loops! I use for loops way more than I use while loops. It seems like most of the time I’m reading items from a database: for loops are perfect for that. I tend to use while loops in Java programs where I need to execute some code until the program terminates; that’s not realistic in PHP. In web-programming, I almost exclusively use for and foreach loops. In most programming languages, the for loop follows the following syntax: for (exp_before; exp_before_loop_body; exp_after) { loop_body } Once PHP encounters the for loop, it looks at the expression exp_before to initialize the necessary variables that are required by the loop. When
Tag: PHP Loops
What do you do while Doing Do-While Loops? A regular while loop will skip over the loop body if the expression that’s evaluated is never true. But what if we need the contents of the body to be executed at least once? That’s what the do-while loop does for us. <?php $will_execute = false; while ( $will_execute ) { echo “I will execute at least once”; } ?> Looking at the code above, the while loop will never execute since the expression is always false. PHP will just skip over it. In this example, we want the statement to execute at least once. We want PHP to