PHP - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

PHP While Loop

PHP While Loop


shape Description

Computers are great at crunching piles of data, and piles of data usually takes the form of sequences, streams, tables and so on. Such data sets usually need to be iterated over. At the very least, some statements may need to be executed more than once. Loops in C++ and iteration statements are used for this purpose. PHP While loop can be of different categories like

While Loop

shape Description

PHP While loop is considered when it is unknown how many times to execute. Compiler only checks the condition is true or false.The While loop executes the statement only when the expression becomes True. After the statement is executed, it will once again evaluate the expression and repeat as needed.

shape Example

[php] <!DOCTYPE html> <html> <body> <?php $x = 15; while($x <= 30) { echo "X value is: $x<br>"; $x++; } ?> </body> </html> [/php] Output:


shape Description

Here first the controller executes the block of code, then it will check the condition of the loop and it will repeats the loop till the condition becomes false.

shape Examples

[php] <!DOCTYPE html> <html> <body> <?php $x=15; do{ echo "X value is: $x <br>"; $x++; } while($x<=30); ?> </body> </html> [/php] Output:


shape Key Points

  • Loops performs repeated execution to make a condition become true.
  • While loop executes after the condition becomes true.
  • Do-while loop executes at least once even if the condition is false.