{"id":1974,"date":"2024-05-25T01:18:57","date_gmt":"2024-05-25T01:18:57","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=1974"},"modified":"2024-06-10T09:03:23","modified_gmt":"2024-06-10T09:03:23","slug":"what-is-loops","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/what-is-loops\/","title":{"rendered":"What is Loop?"},"content":{"rendered":"\n<p>Loops in PHP are control structures that allow you to execute a block of code repeatedly based on a condition. They enable you to automate tasks and iterate over arrays or other data structures.<\/p>\n\n\n\n<p><strong>there are several types of loops<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>While Loop<\/strong><\/li>\n\n\n\n<li><strong>Do while Loop<\/strong><\/li>\n\n\n\n<li><strong>For Loop<\/strong><\/li>\n\n\n\n<li><strong>For Each Loop<\/strong><\/li>\n<\/ol>\n\n\n\n<p><strong>While Loops<\/strong><\/p>\n\n\n\n<p>While the loop keeps repeating in action until an associated condition returns a false.<\/p>\n\n\n\n<p><strong>Code<\/strong> <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  &lt;?php\n    $num= 1;\n    while ($num &lt;7)\n    {\n        echo \"ritik hansda\". $num .\"&lt;\/br>\";\n        $num++;\n    }\n    echo \"loop finished\";\n?>\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"394\" height=\"342\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/06\/Screenshot-142.png\" alt=\"\" class=\"wp-image-2003\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/06\/Screenshot-142.png 394w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/06\/Screenshot-142-300x260.png 300w\" sizes=\"auto, (max-width: 394px) 100vw, 394px\" \/><\/figure>\n\n\n\n<p><strong>2. Do while Loop<\/strong><br>Do-while loop is a control structure that executes a block of code once, and then repeatedly executes the block of code as long as the specified condition is true.<\/p>\n\n\n\n<p><strong>Code <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$counter = 1;\n\ndo {\n    echo \"The counter is: $counter &lt;br&gt;\";\n    $counter++;\n} while ($counter &lt;= 5);\n?&gt;\n<\/code><\/pre>\n\n\n\n<p><strong>Output <\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"511\" height=\"308\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-130.png\" alt=\"\" class=\"wp-image-1977\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-130.png 511w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-130-300x181.png 300w\" sizes=\"auto, (max-width: 511px) 100vw, 511px\" \/><\/figure>\n\n\n\n<p><strong>3. For Loop<\/strong><\/p>\n\n\n\n<p>The for loop in PHP enables the execution of a block of code repeatedly, with a defined initialization, condition, and increment (or decrement) statement.<\/p>\n\n\n\n<p><strong>Code <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nfor ($i = 1; $i &lt;= 10; $i++) {\n    echo \"The current value of i is: $i &lt;br&gt;\";\n}\n?&gt;\n\n<\/code><\/pre>\n\n\n\n<p><strong>Output <\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"403\" height=\"367\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-131.png\" alt=\"\" class=\"wp-image-1978\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-131.png 403w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-131-300x273.png 300w\" sizes=\"auto, (max-width: 403px) 100vw, 403px\" \/><\/figure>\n\n\n\n<p><strong>4. For Each<\/strong> <strong>Loop<\/strong><\/p>\n\n\n\n<p>The foreach loop is a control structure used to iterate over arrays and objects. It allows you to execute a block of code for each element in the array or each property in the object.<\/p>\n\n\n\n<p><br><strong>Code<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> &lt;?php\n$colors = array(\"red\", \"green\", \"blue\");\n\nforeach ($colors as $color) {\n    echo \"Color: $color &lt;br>\";\n}\n?>\n<\/code><\/pre>\n\n\n\n<p> <br><strong>Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"415\" height=\"295\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-132.png\" alt=\"\" class=\"wp-image-1979\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-132.png 415w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/05\/Screenshot-132-300x213.png 300w\" sizes=\"auto, (max-width: 415px) 100vw, 415px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Loops in PHP are control structures that allow you to execute a block of code repeatedly based on a condition. They enable you to automate tasks and&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1598],"tags":[1596],"class_list":["post-1974","post","type-post","status-publish","format-standard","hentry","category-what-is-loop","tag-what-is-loop"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=1974"}],"version-history":[{"count":5,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1974\/revisions"}],"predecessor-version":[{"id":2004,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1974\/revisions\/2004"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=1974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=1974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=1974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}