<?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 => 'uk',
  ),
  '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">
  At 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="para">Поданий вище приклад
виведе:</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="para">Поданий вище приклад
виведе:</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="para">Поданий вище приклад
виведе:</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="para">Поданий вище приклад
виведе:</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 on 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">
  Expressions <var class="varname">expr2</var> and <var class="varname">expr3</var> are
  evaluated every iteration. It&#039;s advisable to use simple expressions in these
  places to avoid performance issues. For example, if the number of iterations
  is known in advance, it is better to use a variable instead of a function call
  in <var class="varname">expr2</var>:
 </p>
 <p class="para">
  <div class="informalexample">
   <div class="example-contents">
<div class="annotation-non-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php

$people </span><span style="color: #007700">= [</span><span style="color: #DD0000">'Kalle'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Pierre'</span><span style="color: #007700">];

</span><span style="color: #FF8000">// Bad practice: calling a function on every iteration
</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">getIterationCount</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">' is cool'</span><span style="color: #007700">;
}

</span><span style="color: #FF8000">// Good practice: storing the count in a variable
</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">$peopleCount </span><span style="color: #007700">= </span><span style="color: #0000BB">getIterationCount</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">$peopleCount</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">' is cool'</span><span style="color: #007700">;
}</span></code></pre></div>
   </div>

  </div>
 </p>
 <p class="simpara">
  In the example above, the function is called on every iteration of the
  first loop, even though it always returns the same value. Storing that
  value in a variable, as the second loop does, calls the function once.
  Note that the number of iterations is then fixed: if the array is
  modified inside the loop, the stored value no longer reflects its size.
 </p>

 <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
  <p class="simpara">
   The size of an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> is stored with the array, which means that
   calling the built-in function <span class="function"><a href="function.count.php" class="function">count()</a></span> does not require
   counting the elements and does not cause performance issues. This does not
   apply to <strong><code><a href="array.constants.php#constant.count-recursive">COUNT_RECURSIVE</a></code></strong>, which walks the whole array.
   Care should also be taken when using <span class="function"><a href="function.count.php" class="function">count()</a></span> on objects
   implementing <span class="interfacename"><a href="class.countable.php" class="interfacename">Countable</a></span>, as such calls can be
   more expensive.
  </p>
 </p></blockquote>

 <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
  <p class="simpara">
   The <code class="literal">for</code> loop is not the recommended way to
   iterate over arrays. The <a href="control-structures.foreach.php" class="link">foreach</a> loop is specifically
   designed for this purpose and is usually more convenient.
  </p>
 </p></blockquote>
</div><?php manual_footer($setup); ?>