<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.control-structures.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'control-structures.for.php',
    1 => 'for',
    2 => 'for',
  ),
  'up' => 
  array (
    0 => 'language.control-structures.php',
    1 => 'Control Structures',
  ),
  'prev' => 
  array (
    0 => 'control-structures.do.while.php',
    1 => 'do-while',
  ),
  'next' => 
  array (
    0 => 'control-structures.foreach.php',
    1 => 'foreach',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/control-structures/for.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="control-structures.for" class="sect1">
 <h2 class="title">for</h2>
 <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p>
 <p class="para">
  <code class="literal">for</code> loops are the most complex loops in PHP.
  They behave like their C counterparts.  The syntax of a
  <code class="literal">for</code> loop is:
  <div class="informalexample">
   <div class="example-contents">
<div class="annotation-interactive cdata"><pre>
for (expr1; expr2; expr3)
    statement
</pre></div>
   </div>

  </div>
 </p>
 <p class="simpara">
  The first expression (<var class="varname">expr1</var>) is
  evaluated (executed) once unconditionally at the beginning of the
  loop.
 </p>
 <p class="simpara">
  In the beginning of each iteration,
  <var class="varname">expr2</var> is evaluated.  If it evaluates to
  <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, the loop continues and the nested
  statement(s) are executed.  If it evaluates to
  <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, the execution of the loop ends.
 </p>
 <p class="simpara">
  At the end of each iteration, <var class="varname">expr3</var> is
  evaluated (executed).
 </p>
 <p class="simpara">
  Each of the expressions can be empty or contain multiple
  expressions separated by commas. In <var class="varname">expr2</var>, all
  expressions separated by a comma are evaluated but the result is taken
  from the last part.
  <var class="varname">expr2</var> being empty means the loop should
  be run indefinitely (PHP implicitly considers it as
  <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, like C).  This may not be as useless as
  you might think, since often you&#039;d want to end the loop using a
  conditional <a href="control-structures.break.php" class="link"><code class="literal">break</code></a>
  statement instead of using the <code class="literal">for</code> truth
  expression.
 </p>
 <p class="para">
  Consider the following examples.  All of them display the numbers
  1 through 10:
  <div class="informalexample">
   <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</span><span style="color: #007700">for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">&lt;= </span><span style="color: #0000BB">10</span><span style="color: #007700">; </span><span style="color: #0000BB">$i</span><span style="color: #007700">++) {
    echo </span><span style="color: #0000BB">$i </span><span style="color: #007700">. </span><span style="color: #DD0000">" "</span><span style="color: #007700">;
}</span></code></pre></div>
   </div>

   <p class="simpara">The above example will output:</p>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
1 2 3 4 5 6 7 8 9 10
</pre></div>
   </div>
  </div>
  <div class="informalexample">
   <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</span><span style="color: #007700">for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">; ; </span><span style="color: #0000BB">$i</span><span style="color: #007700">++) {
    if (</span><span style="color: #0000BB">$i </span><span style="color: #007700">&gt; </span><span style="color: #0000BB">10</span><span style="color: #007700">) {
        break;
    }
    echo </span><span style="color: #0000BB">$i </span><span style="color: #007700">. </span><span style="color: #DD0000">" "</span><span style="color: #007700">;
}</span></code></pre></div>
   </div>

   <p class="simpara">The above example will output:</p>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
1 2 3 4 5 6 7 8 9 10
</pre></div>
   </div>
  </div>
  <div class="informalexample">
   <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
$i </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;
for (; ; ) {
    if (</span><span style="color: #0000BB">$i </span><span style="color: #007700">&gt; </span><span style="color: #0000BB">10</span><span style="color: #007700">) {
        break;
    }
    echo </span><span style="color: #0000BB">$i </span><span style="color: #007700">. </span><span style="color: #DD0000">" "</span><span style="color: #007700">;
    </span><span style="color: #0000BB">$i</span><span style="color: #007700">++;
}</span></code></pre></div>
   </div>

   <p class="simpara">The above example will output:</p>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
1 2 3 4 5 6 7 8 9 10
</pre></div>
   </div>
  </div>
  <div class="informalexample">
   <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</span><span style="color: #007700">for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">$j </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">&lt;= </span><span style="color: #0000BB">10</span><span style="color: #007700">; </span><span style="color: #0000BB">$j </span><span style="color: #007700">+= </span><span style="color: #0000BB">$i</span><span style="color: #007700">, print </span><span style="color: #0000BB">$i </span><span style="color: #007700">. </span><span style="color: #DD0000">" "</span><span style="color: #007700">, </span><span style="color: #0000BB">$i</span><span style="color: #007700">++);</span></code></pre></div>
   </div>

   <p class="simpara">The above example will output:</p>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
1 2 3 4 5 6 7 8 9 10
</pre></div>
   </div>
  </div>
 </p>
 <p class="simpara">
  Of course, the first example appears to be the nicest one (or
  perhaps the fourth), but you may find that being able to use empty
  expressions in <code class="literal">for</code> loops comes in handy in many
  occasions.
 </p>
 <p class="para">
  PHP also supports the alternate &quot;colon syntax&quot; for
  <code class="literal">for</code> loops.
  <div class="informalexample">
   <div class="example-contents">
<div class="annotation-interactive cdata"><pre>
for (expr1; expr2; expr3):
    statement
    ...
endfor;
</pre></div>
   </div>

  </div>
 </p>
 <p class="simpara">
  It&#039;s a common thing to many users to iterate through arrays like in the
  example below.
 </p>
 <p class="para">
  <div class="informalexample">
   <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</span><span style="color: #FF8000">/*
 * This is an array with some data we want to modify
 * when running through the for loop.
 */
</span><span style="color: #0000BB">$people </span><span style="color: #007700">= array(
    array(</span><span style="color: #DD0000">'name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Kalle'</span><span style="color: #007700">, </span><span style="color: #DD0000">'salt' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">856412</span><span style="color: #007700">),
    array(</span><span style="color: #DD0000">'name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Pierre'</span><span style="color: #007700">, </span><span style="color: #DD0000">'salt' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">215863</span><span style="color: #007700">)
);

for(</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">&lt; </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$people</span><span style="color: #007700">); ++</span><span style="color: #0000BB">$i</span><span style="color: #007700">) {
    </span><span style="color: #0000BB">$people</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">][</span><span style="color: #DD0000">'salt'</span><span style="color: #007700">] = </span><span style="color: #0000BB">random_int</span><span style="color: #007700">(</span><span style="color: #0000BB">100000</span><span style="color: #007700">, </span><span style="color: #0000BB">999999</span><span style="color: #007700">);
}
</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$people</span><span style="color: #007700">);</span></code></pre></div>
   </div>

   <p class="simpara">The above example will output
something similar to:</p>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
array(2) {
  [0]=&gt;
  array(2) {
    [&quot;name&quot;]=&gt;
    string(5) &quot;Kalle&quot;
    [&quot;salt&quot;]=&gt;
    int(454478)
  }
  [1]=&gt;
  array(2) {
    [&quot;name&quot;]=&gt;
    string(6) &quot;Pierre&quot;
    [&quot;salt&quot;]=&gt;
    int(776978)
  }
}
</pre></div>
   </div>
  </div>
 </p>
 <p class="simpara">
  The above code can be slow, because the array size is fetched on
  every iteration. Since the size never changes, the loop can be easily
  optimized by using an intermediate variable to store the size instead
  of repeatedly calling <span class="function"><a href="function.count.php" class="function">count()</a></span>:
 </p>
 <p class="para">
  <div class="informalexample">
   <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
$people </span><span style="color: #007700">= array(
    array(</span><span style="color: #DD0000">'name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Kalle'</span><span style="color: #007700">, </span><span style="color: #DD0000">'salt' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">856412</span><span style="color: #007700">),
    array(</span><span style="color: #DD0000">'name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Pierre'</span><span style="color: #007700">, </span><span style="color: #DD0000">'salt' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">215863</span><span style="color: #007700">)
);

for(</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">$size </span><span style="color: #007700">= </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$people</span><span style="color: #007700">); </span><span style="color: #0000BB">$i </span><span style="color: #007700">&lt; </span><span style="color: #0000BB">$size</span><span style="color: #007700">; ++</span><span style="color: #0000BB">$i</span><span style="color: #007700">) {
    </span><span style="color: #0000BB">$people</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">][</span><span style="color: #DD0000">'salt'</span><span style="color: #007700">] = </span><span style="color: #0000BB">random_int</span><span style="color: #007700">(</span><span style="color: #0000BB">100000</span><span style="color: #007700">, </span><span style="color: #0000BB">999999</span><span style="color: #007700">);
}
</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$people</span><span style="color: #007700">);</span></code></pre></div>
   </div>

   <p class="simpara">The above example will output
something similar to:</p>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
array(2) {
  [0]=&gt;
  array(2) {
    [&quot;name&quot;]=&gt;
    string(5) &quot;Kalle&quot;
    [&quot;salt&quot;]=&gt;
    int(454478)
  }
  [1]=&gt;
  array(2) {
    [&quot;name&quot;]=&gt;
    string(6) &quot;Pierre&quot;
    [&quot;salt&quot;]=&gt;
    int(776978)
  }
}
</pre></div>
   </div>
  </div>
 </p>
</div><?php manual_footer($setup); ?>