PHP

PHP Menu

PHP

While Loop - PHP Basics

The PHP while Loop

The while loop repeatedly executes a block of code as long as the specified condition is true.

The syntax for the while loop is as follows:

while (condition is true) {
    code to be executed;
}

Below is an example of the while loop. The code outputs the numbers from 1 to 10.

<?php
$i = 1;
while($i <= 10) { echo " $i <br>"; $i++; }

Exercise

Using the while loop print the numbers from 5 to 1 (decreasing order).

<?php
$var = 123;
<?php
$i = 5;
while($i >= 1) { echo " $i <br>"; $i--; }
{ "test_output_contains": [ { "expected":"5", "error_message":"Sorry. Please try again." }, { "expected":"4", "error_message":"Sorry. Please try again." }, { "expected":"3", "error_message":"Sorry. Please try again." }, { "expected":"2", "error_message":"Sorry. Please try again." }, { "expected":"1", "error_message":"Sorry. Please try again." } ], "success_message":"Good job!", "error_message":"Please read the instructions again." }

Introduction

PHP Basics

PHP Advance

PHP OOP

PHP Functions and Methods