{"id":2553,"date":"2023-11-06T00:42:41","date_gmt":"2023-11-05T23:42:41","guid":{"rendered":"https:\/\/bornoe.org\/blog\/?p=2553"},"modified":"2024-04-17T22:51:16","modified_gmt":"2024-04-17T21:51:16","slug":"php-loops-cheat-sheet","status":"publish","type":"post","link":"https:\/\/bornoe.org\/blog\/2023\/11\/php-loops-cheat-sheet\/","title":{"rendered":"PHP loops cheat sheet"},"content":{"rendered":"\n<p>This cheat sheet provides a quick reference guide for various types of loops in PHP. Loops are essential for executing a block of code repeatedly, allowing for efficient and streamlined programming. The following sections outline the syntax and provide examples for each type of loop, along with the usage of break and continue statements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">For loop<\/h2>\n\n\n\n<p>The for loop is ised to execute a block of code a specific number of times.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>for (initialization; condition; increment\/decrement) {\n    \/\/ Code to be executed\n}   <\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>for ($i = 0; $i &lt; 5; $i++) {\n    echo \"The number is: $i &lt;br&gt;\";\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">The number is: 0<br>The number is: 1<br>The number is: 2<br>The number is: 3<br>The number is: 4<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">While loop<\/h2>\n\n\n\n<p>The while loop executes a block of code as long as the specified condition is true.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>while (condition) {\n    \/\/ Code to be executed\n    \/\/ Increment or decrement variable inside the loop if necessary\n}     <\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$i = 0;\n\nwhile ($i &lt; 5) {\n    echo \"The number is: $i &lt;br&gt;\";\n    $i++;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">The number is: 0<br>The number is: 1<br>The number is: 2<br>The number is: 3<br>The number is: 4<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Do-While loop<\/h2>\n\n\n\n<p>The do-while loop is similar to the while loop, but the code is executed at least once before the condition is checked.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>do {\n    \/\/ Code to be executed\n    \/\/ Increment or decrement variable inside the loop if necessary\n    } while (condition);      <\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$i = 0;\n\ndo {\n    echo \"The number is: $i &lt;br&gt;\";\n    $i++;\n    } while ($i &lt; 5);<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">The number is: 0<br>The number is: 1<br>The number is: 2<br>The number is: 3<br>The number is: 4<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Foreach loop (for arrays)<\/h2>\n\n\n\n<p>The foreach loop (for arrays) iterates through each element in an array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>foreach ($array as $value) {\n    \/\/ Code to be executed\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$colors = array(\"Red\", \"Green\", \"Blue\");\n\nforeach ($colors as $value) {\n    echo \"$value &lt;br&gt;\";\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Red<br>Green<br>Blue<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Foreach loop (for associative arrays)<\/h2>\n\n\n\n<p>The foreach loop (for associative arrays) Iterates through each element in an associative array, providing both the key and the value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>foreach ($array as $key =&gt; $value) {\n    \/\/ Code to be executed\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$age = array(\"Peter\" =&gt; \"35\", \"Ben\" =&gt; \"37\", \"Joe\" =&gt; \"43\");\n\nforeach ($age as $key =&gt; $value) {\n    echo \"$key is $value years old &lt;br&gt;\";\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Peter is 35 years old<br>Ben is 37 years old<br>Joe is 43 years old<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Break statement<\/h2>\n\n\n\n<p>The break statement terminates the loop when a specified condition is met.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<p>Syntax example using a for loop and if statement.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (initialization; condition; increment\/decrement) {\n    if (condition) { \n    break; \/\/ Terminate the loop\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>In this example, the for loop will terminate when the variable &#8216;i&#8217; reaches 3.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for ($i = 0; $i &lt; 5; $i++) {\n    if ($i == 3) {\n       break; \/\/ Terminate the loop\n    }\n    echo \"The number is: $i &lt;br&gt;\";\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">The number is: 0<br>The number is: 1<br>The number is: 2<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Continue statement<\/h2>\n\n\n\n<p>The continue statement skips the current iteration of a loop based on a specific condition.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<p>Syntax example using a for loop and if statement.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (initialization; condition; increment\/decrement) {\n    if (condition) {\n        continue; \/\/ Skip this iteration\n    }\n    \/\/ Code to be executed\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>This example will output the numbers from 0 to 4, skipping the number 3.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for ($i = 0; $i &lt; 5; $i++) {\n    if ($i == 3) {\n        continue; \/\/ Skip this iteration\n    }\n    echo \"The number is: $i &lt;br&gt;\";\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">The number is: 0 <br>The number is: 1 <br>The number is: 2<br>The number is: 4<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This cheat sheet provides a quick reference guide for various types of loops in PHP. Loops are essential for executing a block of code repeatedly, allowing for efficient and streamlined programming. The following sections outline the syntax and provide examples for each type of loop, along with the usage of break and continue statements. For [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-2553","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/posts\/2553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/comments?post=2553"}],"version-history":[{"count":8,"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/posts\/2553\/revisions"}],"predecessor-version":[{"id":2660,"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/posts\/2553\/revisions\/2660"}],"wp:attachment":[{"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/media?parent=2553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/categories?post=2553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/tags?post=2553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}