{"id":15901,"date":"2020-07-08T18:48:47","date_gmt":"2020-07-08T18:48:47","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=15901"},"modified":"2022-04-28T09:15:22","modified_gmt":"2022-04-28T09:15:22","slug":"php-7-fundamental-tutorial-for-beginners-php-loop","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/php-7-fundamental-tutorial-for-beginners-php-loop\/","title":{"rendered":"PHP 7 Fundamental Tutorial for Beginners &#8211; PHP Loop"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">What is Loops?<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/what-is-loop.png\" alt=\"Diagram explaining what a loop is in PHP: code repeated while a condition is met\" class=\"wp-image-15902\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Loops are used to execute the same block of code again and again, as long as a certain condition is met. The basic idea behind a loop is to automate the repetitive tasks within a program to save the time and effort. PHP supports four different types of loops.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/Parts-php.png\" alt=\"Diagram showing the parts of a PHP loop\" class=\"wp-image-15904\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Loops are used to execute the same block of code again and again, as long as a certain condition is true.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Different Types of Loops in PHP<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/Types-php.png\" alt=\"Overview of PHP loop types: while, do-while, for and foreach\" class=\"wp-image-15903\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>while&nbsp;\u2014<\/strong> loops through a block of code as long as the condition specified evaluates to true.<\/li><li><strong>do\u2026while<\/strong>&nbsp;\u2014 the block of code executed once and then condition is evaluated. If the condition is true the statement is repeated as long as the specified condition is true.<\/li><li><strong>for<\/strong>&nbsp;\u2014 loops through a block of code until the counter reaches a specified number.<\/li><li><strong>foreach<\/strong>&nbsp;\u2014 loops through a block of code for each element in an array.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">PHP while Loop<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The while statement will loops through a block of code as long as the condition specified in the while statement evaluate to true.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/Parts-php-1.png\" alt=\"Diagram of PHP while loop parts and flow\" class=\"wp-image-15906\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax<\/strong><\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p class=\"wp-block-paragraph\">while (<em>condition is true<\/em>) {<br><em>&nbsp;&nbsp;code to be executed<\/em>;<br>}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>This Example of Increment<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/example.png\" alt=\"PHP while loop example incrementing a counter variable and its output\" class=\"wp-image-15907\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:-<\/strong><\/p>\n<\/div><\/div>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/ce2667876fbe6ad3bcff9bd1d1685560.js\"><\/script>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>This Example of Decrement<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/decriment.png\" alt=\"PHP while loop example decrementing a counter variable and its output\" class=\"wp-image-15908\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">PHP do\u2026while Loop<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/do-while-loop.png\" alt=\"Flowchart of a PHP do-while loop running code once before checking the condition\" class=\"wp-image-15910\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>do-while<\/code>&nbsp;loop is a variant of while loop, which evaluates the condition at the end of each loop iteration. With a&nbsp;<code>do-while<\/code>&nbsp;loop the block of code executed once, and then the condition is evaluated, if the condition is true, the statement is repeated as long as the specified condition evaluated to is true.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/do-while-.png\" alt=\"PHP do-while loop syntax diagram\" class=\"wp-image-15911\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">do {\n<em>&nbsp; code to be executed;\n<\/em>} while (<em>condition is true<\/em>);\n\n\nThe following example define a loop that starts with&nbsp;<code>$a=1<\/code>. It will then increase&nbsp;<code>$a<\/code>&nbsp;with 1, and print the output. Then the condition is evaluated, and the loop will continue to run as long as&nbsp;$a&nbsp;is less than, or equal to 10.<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Examples<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/98a344b5d99cf561eece1433dce116e4.js\"><\/script>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><strong>This Example of Increment<\/strong><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/do-example.png\" alt=\"PHP do-while example incrementing $a from 1 up to 10 with output\" class=\"wp-image-15912\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>This Example of Decrement<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/Do.decrement.png\" alt=\"PHP do-while example decrementing a counter with output\" class=\"wp-image-15913\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">PHP for Loop<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>for<\/code>&nbsp;loop repeats a block of code as long as a certain condition is met. It is typically used to execute a block of code for certain number of times.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax<\/strong><\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p class=\"wp-block-paragraph\">for(initialization; condition; increment){<br>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Code to be executed<br>}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The parameters of&nbsp;<code>for<\/code>&nbsp;loop have following meanings:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/for-loop.png\" alt=\"PHP for loop syntax showing initialization, condition and increment parts\" class=\"wp-image-15965\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li><code>initialization<\/code>&nbsp;\u2014 it is used to initialize the counter variables, and evaluated once unconditionally before the first execution of the body of the loop.<\/li><li><code>condition<\/code>&nbsp;\u2014 in the beginning of each iteration, condition is evaluated. If it evaluates to&nbsp;<code>true<\/code>, the loop continues and the nested statements are executed. If it evaluates to&nbsp;<code>false<\/code>, the execution of the loop ends.<\/li><li><code>increment<\/code>&nbsp;\u2014 it updates the loop counter with a new value. It is evaluate at the end of each iteration.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The example below defines a loop that starts with&nbsp;<code>$a=1<\/code>. The loop will continue until&nbsp;<code>$a<\/code>&nbsp;is less than, or equal to 10. The variable&nbsp;<code>$a<\/code>&nbsp;will increase by 1 each time the loop runs:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/example-for.png\" alt=\"PHP for loop example printing $a from 1 to 10\" class=\"wp-image-15966\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:-<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/f6910bc9b8bcb4ffc0359aec46720839.js\"><\/script>\n<\/div><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/for-decrement.png\" alt=\"PHP for loop example decrementing a counter with output\" class=\"wp-image-15967\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">PHP foreach Loop<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>foreach<\/code>&nbsp;loop is used to iterate over arrays.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/foreach-array.png\" alt=\"PHP foreach loop example iterating over an array\" class=\"wp-image-15968\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax:-<\/strong><\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p class=\"wp-block-paragraph\">foreach ($<em>array&nbsp;<\/em>as$<em>value<\/em>) {<br>&nbsp;&nbsp;<em>code to be executed;<\/em><br>}<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/exa.png\" alt=\"PHP foreach loop syntax example printing array values\" class=\"wp-image-15969\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The following example demonstrates a loop that will print the values of the given array:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:-<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/3d65b396e9fddcf48e6888edcc01a53e.js\"><\/script>\n<\/div><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/exa1.png\" alt=\"Output of PHP foreach loop printing each value of the array\" class=\"wp-image-15973\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">There is one more syntax of&nbsp;<code>foreach<\/code>&nbsp;loop, which is extension of the first.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax:-<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">foreach($array&nbsp;as&nbsp;$key =&gt; $value){\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Code to be executed\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:-<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/Example-3.png\" alt=\"PHP foreach loop example using key =&gt; value syntax with its output\" class=\"wp-image-15974\"\/><\/figure>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_90812\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_66368\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_90812\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>What is Loops? Loops are used to execute the same block of code again and again, as long as a certain condition is met. The basic idea&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5150],"tags":[],"class_list":["post-15901","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=15901"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15901\/revisions"}],"predecessor-version":[{"id":24681,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15901\/revisions\/24681"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=15901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=15901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=15901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}