Page 190 - CITS - Computer Software Application -TT
P. 190
COMPUTER SOFTWARE APPLICATION - CITS
The default case does not have to be the last case in a switch block:
Example
Putting the default block elsewhere than at the end of the switch block is allowed, but not recommended.
Note: If default is not the last block in the switch block, remember to end the default block with a break statement.
Looping In PHP
In the following chapters you will learn how to repeat code by using loops in PHP.
PHP Loops
Often when you write code, you want the same block of code to run over and over again a certain number of
times. So, instead of adding several almost equal code-lines in a script, we can use loops.
Loops are used to execute the same block of code again and again, as long as a certain condition is true.
In PHP, we have the following loop types:
• while - loops through a block of code as long as the specified condition is true
• do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is
true
• for - loops through a block of code a specified number of times
• foreach - loops through a block of code for each element in an array
The following chapters will explain and give examples of each loop type.
PHP while Loop
The while loop - Loops through a block of code as long as the specified condition is true.
Example:
Print $i as long as $i is less than 6:
Note: remember to increment $i, or else the loop will continue forever.
177
CITS : IT&ITES - Computer Software Application - Lesson 47 - 62