{"id":76,"date":"2026-05-20T08:05:57","date_gmt":"2026-05-20T08:05:57","guid":{"rendered":"https:\/\/gigz.pk\/php\/?post_type=lesson&#038;p=76"},"modified":"2026-05-21T14:38:17","modified_gmt":"2026-05-21T14:38:17","slug":"do-while-loop","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/php\/?lesson=do-while-loop","title":{"rendered":"Do while Loop"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The do while loop in PHP is a control structure used to execute a block of code repeatedly based on a condition. Unlike the while loop, the do while loop guarantees that the code block will run at least one time before checking the condition. This makes it useful when the first execution must happen regardless of the condition result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Do While Loop<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A do while loop is a post tested loop in PHP. It first executes the code and then checks the condition. If the condition is true, the loop continues. If the condition is false, the loop stops.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of Do While Loop<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>do {<br>    \/\/ code to be executed<br>} while (condition);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How Do While Loop Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">First the code inside the do block runs one time. After execution, the condition is checked. If the condition is true, the loop runs again. This process continues until the condition becomes false.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Do While Loop<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$i = 1;<br><br>do {<br>    echo \"Number: \" . $i;<br>    $i++;<br>} while ($i &lt;= 5);<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Number 1 Number 2 Number 3 Number 4 Number 5<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When to Use Do While Loop<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The do while loop is used when:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You want the loop to execute at least once<br>You are validating user input<br>You are working with menus or repeated actions<br>You need post condition checking logic<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Difference Between While Loop and Do While Loop<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">While loop checks condition first then executes code<br>Do while loop executes code first then checks condition<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example with Condition False Initially<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$x = 10;<br><br>do {<br>    echo \"Value: \" . $x;<br>    $x++;<br>} while ($x &lt; 5);<br>?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Even though the condition is false, the loop runs one time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Real World Use Case<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Do while loops are commonly used in:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">User login attempts<br>Menu driven programs<br>Input validation systems<br>Game loops and retries<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Do While Loop<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Ensures at least one execution<br>Simple and easy to understand<br>Useful for input based systems<br>Works well with interactive applications<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Forgetting to update loop variable<br>Creating infinite loops<br>Incorrect condition placement<br>Not understanding post condition logic<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Always update loop control variable<br>Keep conditions simple and clear<br>Avoid unnecessary nested loops<br>Use meaningful variable names<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/php\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PHP Fundamentals (Beginner Level) > Loops > do while Loop<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779264354532\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":17,"template":"","class_list":["post-76","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Do while Loop - Learn PHP with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn PHP do while loop with syntax, examples, and real world usage for beginners in web development training course.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/php\/?lesson=do-while-loop\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Do while Loop - Learn PHP with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn PHP do while loop with syntax, examples, and real world usage for beginners in web development training course.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/php\/?lesson=do-while-loop\" \/>\n<meta property=\"og:site_name\" content=\"Learn PHP with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-21T14:38:17+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=do-while-loop\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=do-while-loop\",\"name\":\"Do while Loop - Learn PHP with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\"},\"datePublished\":\"2026-05-20T08:05:57+00:00\",\"dateModified\":\"2026-05-21T14:38:17+00:00\",\"description\":\"Learn PHP do while loop with syntax, examples, and real world usage for beginners in web development training course.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=do-while-loop#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=do-while-loop\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=do-while-loop#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/php\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP Fundamentals (Beginner Level) > Loops > do while Loop\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/\",\"name\":\"Learn PHP with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Do while Loop - Learn PHP with GiGz.PK","description":"Learn PHP do while loop with syntax, examples, and real world usage for beginners in web development training course.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/php\/?lesson=do-while-loop","og_locale":"en_US","og_type":"article","og_title":"Do while Loop - Learn PHP with GiGz.PK","og_description":"Learn PHP do while loop with syntax, examples, and real world usage for beginners in web development training course.","og_url":"https:\/\/gigz.pk\/php\/?lesson=do-while-loop","og_site_name":"Learn PHP with GiGz.PK","article_modified_time":"2026-05-21T14:38:17+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/php\/?lesson=do-while-loop","url":"https:\/\/gigz.pk\/php\/?lesson=do-while-loop","name":"Do while Loop - Learn PHP with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/php\/#website"},"datePublished":"2026-05-20T08:05:57+00:00","dateModified":"2026-05-21T14:38:17+00:00","description":"Learn PHP do while loop with syntax, examples, and real world usage for beginners in web development training course.","breadcrumb":{"@id":"https:\/\/gigz.pk\/php\/?lesson=do-while-loop#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/php\/?lesson=do-while-loop"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/php\/?lesson=do-while-loop#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/php"},{"@type":"ListItem","position":2,"name":"PHP Fundamentals (Beginner Level) > Loops > do while Loop"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/php\/#website","url":"https:\/\/gigz.pk\/php\/","name":"Learn PHP with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/php\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson\/76","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}