HOW TO WRITE PHP SCRIPTS Loops using while (Multiple domain web hosting)
HOW TO WRITE PHP SCRIPTS Loops using while and do… while The simplest type of loop is called a while loop. Its basic structure looks like this: while (condition is true) { do something } The following code displays every number from 1 through 100 in a browser (you can test it in while.php in the download files for this chapter). It begins by setting a variable ($i) to 1, and then using the variable as a counter to control the loop, as well as display the current number onscreen. $i = 1; // set counter while ($i <= 100) { echo "$i
“; $i++; // increase counter by 1 } A variation of the while loop uses the keyword do and follows this basic pattern: do { code to be executed } while (condition to be tested); The only difference between a do… while loop and a while loop is that the code within the do block is executed at least once, even if the condition is never true. The following code (in dowhile.php) displays the value of $i once, even though it s greater than the maximum expected. $i = 1000; do { echo “$i
“; $i++; // increase counter by 1 } while ($i <= 100); The danger with while and do... while loops is forgetting to set a condition that brings the loop to an end, or setting an impossible condition. When this happens, you create an infinite loop that either freezes your computer or causes the browser to crash. The versatile for loop The for loop is less prone to generating an infinite loop because you are required to declare all the conditions of the loop in the first line. The for loop uses the following basic pattern: for (initialize counter; test; increment) { code to be executed }
We recommend high quality webhost to host and run your jsp application: christian web host services.