Mastering the do-while Loop in PHP: A Complete Guide

Rumman Ansari   Software Engineer   2024-07-18 09:19:44   5494  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

The do-while loop is very similar to the while loop, but it does things in reverse order. The mechanism of the while loop is - while a condition is true, perform a certain action. The mechanism of the do-while loop is - perform a certain action while a condition is true.

Syntax:



do{ 
// execute this code;
}

while (condition);


Example:



<?php
$num = 0;

do{ 
$num = $num + 5;
print $num . "<br />";
}

while ($num < 25); 
?>


Output:

This will produce the following result



5
10
15
20
25


Flow chart

 do-while Loops
Figure: do-while Loops

In the above code, a variable named num is initialized with the value of 5. The condition in the do-while loop is that while num is less than 25, 5 should be added to num. Once the value of num is greater than 25, the loop will stop executing.

MCQ Available

There are 4 MCQs available for this topic.

4 MCQ