{"id":392,"date":"2018-07-16T02:43:44","date_gmt":"2018-07-16T02:43:44","guid":{"rendered":"https:\/\/www.go4expert.com\/tutorials\/?p=392"},"modified":"2018-08-02T02:49:34","modified_gmt":"2018-08-02T02:49:34","slug":"loops","status":"publish","type":"post","link":"https:\/\/www.go4expert.com\/tutorials\/loops\/","title":{"rendered":"PHP Loops"},"content":{"rendered":"<p>In programming, a loop consists of a series of instructions that are repeatedly executed until a condition is met. Loops are required in PHP because sometimes we need to execute the same block of code again and again in a program until a certain condition is met. Hence, instead of writing several lines of repetitive code, we can use a loop.<\/p>\n<p>A loop attempts to automate repetitive tasks within a program because we need to save both time and effort. PHP supports the following four different types of loops.<\/p>\n<ul>\n<li><b>While<\/b> \u2014 This command enables us to loop through a block of code until a specified condition is true.<br \/>\n<b><\/b><\/li>\n<li><b>Do\u2026while<\/b> \u2014 Using the do...while statement a block of code can be executed once unconditionally and then a condition is evaluated. If the condition is true then the block of code will be repeatedly executed as long as the specified condition is true.<br \/>\n<b><\/b><\/li>\n<li><b>For<\/b> \u2014 This loops through a block of PHP code a specified number of times which is mentioned in the statement.<\/li>\n<li><b>Foreach<\/b> \u2014 This repeatedly executes a block of PHP code for every element in an array.<\/li>\n<\/ul>\n<h2>The while loop in PHP<\/h2>\n<p>Using the while statement a block of PHP code will be repeatedly executed as long as the condition in the while statement evaluates to true. It is called the while statement because the block of code is executed while the condition is true.<\/p>\n<pre><div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">while(condition){\r\n\u00a0 \u00a0 \/\/ Code to be executed\r\n}<\/textarea><\/div><\/pre>\n<p>The example below shows a loop that starts at $a=1. This loop will continue to run as long as variable $a is less than or equal to 5. Hence the loop will be executed 5 times. Every time the loop executes $a will be increased by 1 and the condition will be checked at the start of the next loop.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre><div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\r\n$a = 1;\r\nwhile($a &lt;= 5){\r\n\u00a0 \u00a0 $a++;\r\n\u00a0 \u00a0 echo \"The number is \" . $a;\r\n}\r\n?&gt;<\/textarea><\/div><\/pre>\n<h2>do\u2026while Loop in PHP<\/h2>\n<p>The do...while statement is a deviation of the while loop, which evaluates the condition at the end of each loop iteration.<\/p>\n<p>The do...while loop executes a block of code at least once unconditionally, and then repeats the loop (i.e. executes the statements in the loop)as long as the specified condition evaluates to true.<\/p>\n<pre><div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">do{\r\n\u00a0 \u00a0 \/\/ Code to be executed\r\n}\r\nwhile(condition);<\/textarea><\/div><\/pre>\n<p>The example below shows the implementation of a do\u2026while loop. The variable whose value will be evaluated in the condition is initialized to 5. Everytime that the loop executes $a is incremented by one and the value of $a is displayed. The condition is evaluated at the end of the loop execution because of which the loop executes at least once. The loop will continue to run as long as $a is less than, or equal to 8. Hence the loop executes a total of 4 times.<\/p>\n<p><b>Example: <\/b><\/p>\n<pre><div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\r\n$a = 5;\r\ndo{\r\n\u00a0 \u00a0 $a++;\r\n\u00a0 \u00a0 echo \"The number is \" . $a . \"\";\r\n}\r\nwhile($a &lt;= 8);\r\n?&gt;<\/textarea><\/div><\/pre>\n<h2>Difference between while and do\u2026while Loop<\/h2>\n<p>In the While loop, the condition is evaluated at the beginning of each loop iteration. If the conditional expression evaluates to false at the start itself, then the loop will never be executed.<\/p>\n<p>Using the do\u2026while loop the loop will execute at least once, even when the conditional expression is false because the condition is evaluated at the end of the loop iteration rather than at the beginning.<\/p>\n<h2>The For loop in PHP<\/h2>\n<p>The for loop repeatedly executes a block of code until the specified condition is met. Because of this it is usually used when we already know the number of times that the block of code needs to be repeatedly executed.<\/p>\n<pre><div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">for (initialization; condition; increment){\r\n\u00a0 \u00a0 \/\/ Code to be executed\r\n}<\/textarea><\/div><\/pre>\n<p>The for statement syntax consists of the following 3 parts each of which has a specific meaning<\/p>\n<ul>\n<li><strong>Initialization<\/strong> \u2013 It is so called because it is used to initialize the counter variables, and it is executed just once unconditionally before the body of the loop is executed for the first time.<\/li>\n<li><strong>Condition<\/strong> - At the beginning of every iteration, the condition is evaluated. If the condition is true, the loop continues and executes the nested statements within the for the loop. If the condition is false, the execution of the loop ends because the condition fails.<\/li>\n<li><strong>Increment<\/strong> - This will update the loop counter with a new value based on the operation specified. This will be executed at the end of every iteration. It is so called because the operation usually performed is the increment operation. However, any other arithmetic operations could also be performed.<\/li>\n<\/ul>\n<p>The example below defines a loop that starts at $a=1. The loop will continue to execute until $a is less than, or equal to 5 because of the condition which is specified. The variable $a will increase by 1 each time the loop executes because of the increment part of the for loop\u00a0statement. Hence the loop will execute 5 times.<\/p>\n<p><b>Example: <\/b><\/p>\n<pre><div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\r\nfor($a=1; $a&lt;=5; $a++){\r\n\u00a0 \u00a0 echo \"The number is \" . $a . \"\";\r\n}\r\n?&gt;<\/textarea><\/div><\/pre>\n<h2>PHP foreach Loop<\/h2>\n<p>The foreach loop is used to iterate over arrays. It is called as a foreach loop because it enables us to execute a set of statements for every element of an array.<\/p>\n<pre><div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">foreach ($array as $value){\r\n\u00a0 \u00a0 \/\/ Code to be executed\r\n}<\/textarea><\/div><\/pre>\n<p>The example below demonstrates a loop that will print the values of the given array:<\/p>\n<p><b>Example:<\/b><\/p>\n<pre><div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\r\n$color = array(\"Blue\", \"Red\", \"Green\");\r\n\r\n\/\/ Loop through the color array by using foreach\r\nforeach($color as $value){\r\n\u00a0 \u00a0 echo $value . \"&lt;br \/&gt;\";\r\n}\r\n?&gt;<\/textarea><\/div><\/pre>\n<p>There is another syntax of the foreach loop (which is an extension of the first) because we need to handle different types of arrays.<\/p>\n<pre><div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">foreach($array as $key =&gt; $value){\r\n\u00a0 \u00a0 \/\/ Code to be executed\r\n}<\/textarea><\/div><\/pre>\n<p><b>Example: <\/b><\/p>\n<pre><div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\r\n$winner = array(\r\n\u00a0 \u00a0 \"name\" =&gt; \"David John\",\r\n\u00a0 \u00a0 \"email\" =&gt; \"JohnDavid@mail.com\",\r\n\u00a0 \u00a0 \"age\" =&gt; 21\r\n);\r\n\r\n\/\/ Loop through winner array and use a different syntax because of the different type of array\r\nforeach($winner as $key =&gt; $value){\r\n\u00a0 \u00a0 echo $key . \" : \" . $value . \"\";\r\n}\r\n?&gt;<\/textarea><\/div><\/pre>\n<p>Here the echo statement is executed for every element of the array $winner<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP loops allow you to execute selected statements repeatedly based on the outcome of some test conditions. Read all about the different loop statements.<\/p>\n","protected":false},"author":6,"featured_media":210,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[7],"tags":[],"class_list":{"0":"post-392","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-php","8":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP Loops - Go4Expert Tutorials<\/title>\n<meta name=\"description\" content=\"PHP loops allow you to execute selected statements repeatedly based on the outcome of some test conditions. Read all about the different loop statements.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.go4expert.com\/tutorials\/loops\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP Loops - Go4Expert Tutorials\" \/>\n<meta property=\"og:description\" content=\"PHP loops allow you to execute selected statements repeatedly based on the outcome of some test conditions. Read all about the different loop statements.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.go4expert.com\/tutorials\/loops\/\" \/>\n<meta property=\"og:site_name\" content=\"Go4Expert Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-16T02:43:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-08-02T02:49:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.go4expert.com\/tutorials\/wp-content\/uploads\/2018\/07\/PHP.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"770\" \/>\n\t<meta property=\"og:image:height\" content=\"385\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"vnlaks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"vnlaks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/loops\/\",\"url\":\"https:\/\/www.go4expert.com\/tutorials\/loops\/\",\"name\":\"PHP Loops - Go4Expert Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#website\"},\"datePublished\":\"2018-07-16T02:43:44+00:00\",\"dateModified\":\"2018-08-02T02:49:34+00:00\",\"author\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/7f62b1e7d9b60341013fb7bd42ca5aae\"},\"description\":\"PHP loops allow you to execute selected statements repeatedly based on the outcome of some test conditions. Read all about the different loop statements.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/loops\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.go4expert.com\/tutorials\/loops\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/loops\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.go4expert.com\/tutorials\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP Loops\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#website\",\"url\":\"https:\/\/www.go4expert.com\/tutorials\/\",\"name\":\"Go4Expert Tutorials\",\"description\":\"Programming And Web Development Tutorials\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.go4expert.com\/tutorials\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/7f62b1e7d9b60341013fb7bd42ca5aae\",\"name\":\"vnlaks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d1b367cbd6665aad2f29e76d2ab4f94adb74ab0f6e2422cd2c75f8645c59f903?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d1b367cbd6665aad2f29e76d2ab4f94adb74ab0f6e2422cd2c75f8645c59f903?s=96&d=mm&r=g\",\"caption\":\"vnlaks\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP Loops - Go4Expert Tutorials","description":"PHP loops allow you to execute selected statements repeatedly based on the outcome of some test conditions. Read all about the different loop statements.","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:\/\/www.go4expert.com\/tutorials\/loops\/","og_locale":"en_US","og_type":"article","og_title":"PHP Loops - Go4Expert Tutorials","og_description":"PHP loops allow you to execute selected statements repeatedly based on the outcome of some test conditions. Read all about the different loop statements.","og_url":"https:\/\/www.go4expert.com\/tutorials\/loops\/","og_site_name":"Go4Expert Tutorials","article_published_time":"2018-07-16T02:43:44+00:00","article_modified_time":"2018-08-02T02:49:34+00:00","og_image":[{"width":770,"height":385,"url":"https:\/\/www.go4expert.com\/tutorials\/wp-content\/uploads\/2018\/07\/PHP.jpg","type":"image\/jpeg"}],"author":"vnlaks","twitter_card":"summary_large_image","twitter_misc":{"Written by":"vnlaks","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.go4expert.com\/tutorials\/loops\/","url":"https:\/\/www.go4expert.com\/tutorials\/loops\/","name":"PHP Loops - Go4Expert Tutorials","isPartOf":{"@id":"https:\/\/www.go4expert.com\/tutorials\/#website"},"datePublished":"2018-07-16T02:43:44+00:00","dateModified":"2018-08-02T02:49:34+00:00","author":{"@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/7f62b1e7d9b60341013fb7bd42ca5aae"},"description":"PHP loops allow you to execute selected statements repeatedly based on the outcome of some test conditions. Read all about the different loop statements.","breadcrumb":{"@id":"https:\/\/www.go4expert.com\/tutorials\/loops\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.go4expert.com\/tutorials\/loops\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.go4expert.com\/tutorials\/loops\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.go4expert.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"PHP Loops"}]},{"@type":"WebSite","@id":"https:\/\/www.go4expert.com\/tutorials\/#website","url":"https:\/\/www.go4expert.com\/tutorials\/","name":"Go4Expert Tutorials","description":"Programming And Web Development Tutorials","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.go4expert.com\/tutorials\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/7f62b1e7d9b60341013fb7bd42ca5aae","name":"vnlaks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d1b367cbd6665aad2f29e76d2ab4f94adb74ab0f6e2422cd2c75f8645c59f903?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d1b367cbd6665aad2f29e76d2ab4f94adb74ab0f6e2422cd2c75f8645c59f903?s=96&d=mm&r=g","caption":"vnlaks"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.go4expert.com\/tutorials\/wp-content\/uploads\/2018\/07\/PHP.jpg","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/s9JSSi-loops","_links":{"self":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/392","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/comments?post=392"}],"version-history":[{"count":11,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/392\/revisions"}],"predecessor-version":[{"id":429,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/392\/revisions\/429"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/media\/210"}],"wp:attachment":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/media?parent=392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/categories?post=392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/tags?post=392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}