{"id":40,"date":"2021-03-07T14:17:46","date_gmt":"2021-03-07T14:17:46","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=40"},"modified":"2025-04-06T01:48:19","modified_gmt":"2025-04-06T01:48:19","slug":"php-for-loop","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-for-loop\/","title":{"rendered":"PHP for"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about PHP <code>for<\/code> statement to execute a block of code repeatedly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-php-for-statement'>Introduction to PHP for statement <a href=\"#introduction-to-php-for-statement\" class=\"anchor\" id=\"introduction-to-php-for-statement\" title=\"Anchor for Introduction to PHP for statement\">#<\/a><\/h2>\n\n\n\n<p>The <code>for<\/code> statement allows you to execute a code block repeatedly. The syntax of the <code>for<\/code> statement is as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">for<\/span> (start; condition; increment) {\n\tstatement;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>start<\/code> is evaluated <strong>once <\/strong>when the loop starts.<\/li>\n\n\n\n<li>The <code>condition<\/code> is evaluated once in each iteration. If the <code>condition<\/code> is <code>true<\/code>, the <code>statement<\/code> in the body is executed. Otherwise, the loop ends.<\/li>\n\n\n\n<li>The <code>increment<\/code> expression is evaluated once after each iteration.<\/li>\n<\/ul>\n\n\n\n<p>PHP allows you to specify multiple expressions in the <code>start<\/code>, <code>condition<\/code>, and <code>increment<\/code> of the <code>for<\/code> statement.<\/p>\n\n\n\n<p>In addition, you can leave the <code>start<\/code>, <code>condition<\/code>, and <code>increment<\/code> empty, indicating that PHP should do nothing for that phase.<\/p>\n\n\n\n<p>The following flowchart illustrates how the <code>for<\/code> statement works:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"198\" height=\"528\" src=\"https:\/\/phptutorial.net\/wp-content\/uploads\/2021\/03\/php-for.png\" alt=\"\" class=\"wp-image-405\" srcset=\"https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/03\/php-for.png 198w, https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/03\/php-for-113x300.png 113w\" sizes=\"auto, (max-width: 198px) 100vw, 198px\" \/><\/figure>\n<\/div>\n\n\n<p>When you leave all three parts empty, you should use a <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-break\/\">break<\/a><\/code> statement to exit the loop at some point. Otherwise, you&#8217;ll have an infinite loop:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">for<\/span> (; ;) {\n\t<span class=\"hljs-comment\">\/\/ do something<\/span>\n\t<span class=\"hljs-comment\">\/\/ ...<\/span>\n\n\t<span class=\"hljs-comment\">\/\/ exit the loop<\/span>\n\t<span class=\"hljs-keyword\">if<\/span> (condition) {\n\t\t<span class=\"hljs-keyword\">break<\/span>;\n\t}\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='php-for-statement-example'>PHP for statement example <a href=\"#php-for-statement-example\" class=\"anchor\" id=\"php-for-statement-example\" title=\"Anchor for PHP for statement example\">#<\/a><\/h2>\n\n\n\n<p>The following shows a simple example that adds numbers from 1 to 10:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$total = <span class=\"hljs-number\">0<\/span>;\n\n<span class=\"hljs-keyword\">for<\/span> ($i = <span class=\"hljs-number\">1<\/span>; $i &lt;= <span class=\"hljs-number\">10<\/span>; $i++) {\n\t$total += $i;\n}\n\n<span class=\"hljs-keyword\">echo<\/span> $total;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiR0b3RhbCA9IDA7Cgpmb3IgKCRpID0gMTsgJGkgPD0gMTA7ICRpKyspIHsKCSR0b3RhbCArPSAkaTsKfQoKZWNobyAkdG90YWw7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-number\">55<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, initialize the <code>$total<\/code> to zero.<\/li>\n\n\n\n<li>Second, start the loop by setting the variable <code>$i<\/code> to 1. This initialization will be evaluated once when the loop starts.<\/li>\n\n\n\n<li>Third, the loop continues as long as <code>$i<\/code> is less than or equal to <code>10<\/code>. The expression <code>$i &lt;= 10<\/code> is evaluated once after every iteration.<\/li>\n\n\n\n<li>Fourth, the expression <code>$i++<\/code> is evaluated after each iteration.<\/li>\n\n\n\n<li>Finally, the loop runs exactly <code>10<\/code> iterations and stops once <code>$i<\/code> becomes <code>11<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='alternative-syntax-of-the-for-statement'>Alternative syntax of the for statement <a href=\"#alternative-syntax-of-the-for-statement\" class=\"anchor\" id=\"alternative-syntax-of-the-for-statement\" title=\"Anchor for Alternative syntax of the for statement\">#<\/a><\/h2>\n\n\n\n<p>The for statement has the alternative syntax as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">for<\/span> (start; condition; increment):\n   statement;\n<span class=\"hljs-keyword\">endfor<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following script uses the alternative syntax to calculate the sum of 10 numbers from 1 to 10:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$total = <span class=\"hljs-number\">0<\/span>;\n\n<span class=\"hljs-keyword\">for<\/span> ($i = <span class=\"hljs-number\">1<\/span>; $i &lt;= <span class=\"hljs-number\">10<\/span>; $i++):\n\t$total += $i;\n<span class=\"hljs-keyword\">endfor<\/span>;\n\n<span class=\"hljs-keyword\">echo<\/span> $total;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiR0b3RhbCA9IDA7Cgpmb3IgKCRpID0gMTsgJGkgPD0gMTA7ICRpKyspOgoJJHRvdGFsICs9ICRpOwplbmRmb3I7CgplY2hvICR0b3RhbDs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-number\">55<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the PHP <code>for<\/code> statement to execute a code block a specified number of times.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"40\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-for-loop\/\"\n\t\t\t\tdata-post-title=\"PHP for\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"40\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-for-loop\/\"\n\t\t\t\tdata-post-title=\"PHP for\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn about PHP for statement to execute a block of code repeatedly. Introduction to PHP for statement # The for statement allows you to execute a code block repeatedly. The syntax of the for statement is as follows: How it works. PHP allows you to specify multiple expressions in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":28,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-40","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/40\/revisions"}],"predecessor-version":[{"id":3001,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/40\/revisions\/3001"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}