PHP while Loop

Summary: in this tutorial, you will learn about PHP while loop statement that allows you to run the same block of code repeatedly until a condition is met.

Introduction to PHP while loop

The while loop is the simplest loop statement in PHP.

The following illustrates the syntax of the while loop statement.

The while loop statement checks the expression at the beginning of each iteration. If the expression evaluates to true, the code block inside the curly braces is executed. If the expression evaluates to false, the loop exits.

PHP while Loop

PHP while loop examples

Here is an example of using while statement.

First, we initialize a counter variable $counter with value 1. Then, inside the loop we display of the current value of the counter and increase its value by 1. The while loop statement checks the value of the counter variable again and executes the code until the value of counter variable reaches 10.

In this tutorial, we’ve discussed about the PHP while loop statement that allows you to execute a code block again and again based on a condition.

  • Was this tutorial helpful ?
  • YesNo