Concept
How are while loops structured and styled in PHP?
ComputerScienceOne / While Loops
"PHP supports while loops, for loops, and do-while loops using the keywords while, for, and do (along with another while). Continuation conditions for loops are enclosed in parentheses, (...) and blocks of code associated with the loop are enclosed in curly brackets. Code Sample 39.1 contains an example of a basic while loop in PHP. As with conditional statements, our code style places the opening curly bracket on the same line as the while keyword and continuation condition. The inner block of code is also indented and all lines in the block are indented to the same level. In addition, the continuation condition does not contain a semicolon since it is not an executable statement. What happens is:\n\n1 $i = 1; //Initialization\n\n2 while($i <= 10) { //continuation condition\n\n3 //perform some action\n\n4 $i++; //iteration\n\n5 }\n\nCode Sample 39.1.: While Loop in PHP 527"
Related Ideas
- How does a do-while loop differ from a while loop in PHP?ComputerScienceOne · Do-While Loops
- How is a basic while loop structured in Java?ComputerScienceOne · While Loops
- How are for loops structured in PHP?ComputerScienceOne · For Loops
- How are foreach loops used to iterate over array elements in PHP?ComputerScienceOne · Do-While Loops
- What happens when a while loop never increments its counter?ComputerScienceOne · For Loops
- How does a flag-controlled while loop determine when to stop?ComputerScienceOne · For Loops
- Why should you avoid a semicolon after a while condition?ComputerScienceOne · While Loops
- How do if, if-else, and if-else-if statements work in PHP?ComputerScienceOne · If, If-Else, If-Else-If Statements