do-while loop in PHP

Rumman Ansari   Software Engineer   2023-03-09   5490 Share
☰ Table of Contents

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


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.