The do...while Statement - PHP

You can delay the decision to continue executing a loop until the end by using a do...while statement. retools. You won't notice a difference unless you run the script on a Friday. On Fridays the original will print nothing in its list of days. The new version will put Friday in the list because the body of the loop is executed before currentDate is tested. By switching to a do...while loop, the loop now lists the days until next Friday.

Using do...while to Print Day Names

** get the current date in number of seconds
$currentDate = time();
** print some text explaining the output
print("Days left before next Friday: ");
print("<OL> ");
** print day name
print("<LI>" . date("l", $currentDate) . " ");
** add 24 hours to currentDate
$currentDate += (60 * 60 * 24);
while(date("l", $currentDate) != "Friday");
print("</OL> ");

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

PHP Topics