The auto-increment and auto-decrement operators -------------------------------------------------------------- In our explanation of the "for" loop above, we mentioned the auto-increment operator [++] in passing. The auto-increment operator is a PHP operator designed to automatically increment the value of the variable it is attached to by 1. This snippet of code should explain it. And there's also a corresponding auto-decrement operator [--], which does exactly the opposite: These operators are frequently used in loops to automatically update the value of the loop counter. break and continue ------------------------- When dealing with loops, there are two important keywords you should be aware of: "break" and "continue". The "break" keyword is used to exit a loop when it encounters an unexpected situation. A good example of this is the dreaded "division by zero" error - when dividing one number by another one[which keeps decreasing], it is advisable to check the divisor and use the "break" statement to exit the loop as soon as it becomes equal to zero. The "continue" keyword is used to skip a particular iteration of the loop and move to the next iteration immediately - it's demonstrated in the following example: In this case, PHP will print a string of numbers from 1 to 10 - however, when it hits 7, the "continue" statement will cause it to skip that particular iteration and go back to the top of the loop. So your string of numbers will not include 7 - try it and see for yourself. And that's all we have time for this week. We'll be back soon with more PHP 101, same time, same channel. Make sure you tune in!
blog comments powered by Disqus |