<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.array.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.range.php',
    1 => 'range',
    2 => 'Create an array containing a range of elements',
  ),
  'up' => 
  array (
    0 => 'ref.array.php',
    1 => 'Array Functions',
  ),
  'prev' => 
  array (
    0 => 'function.prev.php',
    1 => 'prev',
  ),
  'next' => 
  array (
    0 => 'function.reset.php',
    1 => 'reset',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/array/functions/range.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.range" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">range</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">range</span> &mdash; <span class="dc-title">Create an array containing a range of elements</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.range-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>range</strong></span>(<span class="methodparam"><span class="type"><span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.float.php" class="type float">float</a></span></span> <code class="parameter">$start</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.float.php" class="type float">float</a></span></span> <code class="parameter">$end</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.float.php" class="type float">float</a></span></span> <code class="parameter">$step</code><span class="initializer"> = 1</span></span>): <span class="type"><a href="language.types.array.php" class="type array">array</a></span></div>

  <p class="para rdfs-comment">
   Create an array containing a range of elements.
  </p>
  <p class="para">
   If both <code class="parameter">start</code> and <code class="parameter">end</code> are
   <span class="type"><a href="language.types.string.php" class="type string">string</a></span>s, and <code class="parameter">step</code> is <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>
   the produced array will be a sequence of bytes.
   Otherwise, the produced array will be a sequence of numbers.
  </p>
  <p class="para">
   The sequence is increasing if <code class="parameter">start</code> is less than
   equal to <code class="parameter">end</code>.
   Otherwise, the sequence is decreasing.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.range-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">start</code></dt>
     <dd>
      <p class="para">
       First value of the sequence.
      </p>
     </dd>
    
    
     <dt><code class="parameter">end</code></dt>
     <dd>
      <p class="para">
       Last possible value of the sequence.
      </p>
     </dd>
    
    
     <dt><code class="parameter">step</code></dt>
     <dd>
      <p class="para">
       <code class="parameter">step</code> indicates by how much is the produced
       sequence progressed between values of the sequence.
      </p>
      <p class="para">
       <code class="parameter">step</code> may be negative for decreasing sequences.
      </p>
      <p class="para">
       If <code class="parameter">step</code> is a <span class="type"><a href="language.types.float.php" class="type float">float</a></span> without a
       fractional part, it is interpreted as <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.range-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns a sequence of elements as an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> with the first
   element being <code class="parameter">start</code> going up to
   <code class="parameter">end</code>, with each value of the sequence being
   <code class="parameter">step</code> values apart.
  </p>
  <p class="para">
   The last element of the returned array is either <code class="parameter">end</code>
   or the previous element of the sequence,
   depending on the value of <code class="parameter">step</code>.
  </p>
  <p class="para">
   If both <code class="parameter">start</code> and <code class="parameter">end</code> are
   <span class="type"><a href="language.types.string.php" class="type string">string</a></span>s, and <code class="parameter">step</code> is <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>
   the produced array will be a sequence of bytes,
   generally latin <abbr class="abbrev">ASCII</abbr> characters.
  </p>
  <p class="para">
   If at least one of <code class="parameter">start</code>, <code class="parameter">end</code>,
   or <code class="parameter">step</code> is <span class="type"><a href="language.types.float.php" class="type float">float</a></span>
   the produced array will be a sequence of <span class="type"><a href="language.types.float.php" class="type float">float</a></span>.
  </p>
  <p class="para">
   Otherwise, the produced array will be a sequence of <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.range-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     If <code class="parameter">step</code> is <code class="literal">0</code>,
     a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> is thrown.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     If <code class="parameter">start</code>, <code class="parameter">end</code>,
     or <code class="parameter">step</code> is not <span class="function"><a href="function.is-finite.php" class="function">is_finite()</a></span>,
     a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> is thrown.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     If <code class="parameter">step</code> is negative,
     but the produced range is increasing
     (i.e. <code class="code">$start &lt;= $end</code>),
     a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> is thrown.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     If <code class="parameter">start</code> or <code class="parameter">end</code>
     is the empty string <code class="literal">&#039;&#039;</code>,
     an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> is emitted and
     the empty string will be interpreted as <code class="literal">0</code>.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     If <code class="parameter">start</code> or <code class="parameter">end</code> is a
     non-<a href="language.types.numeric-strings.php" class="link">numeric string</a>
     with more than one byte, an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> is emitted.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     If <code class="parameter">start</code> or <code class="parameter">end</code> is a string
     that is implicitly cast to an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> because the other boundary
     value is a number, an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> is emitted.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     If <code class="parameter">step</code> is a <span class="type"><a href="language.types.float.php" class="type float">float</a></span>,
     and <code class="parameter">start</code> and <code class="parameter">end</code> are
     non-<a href="language.types.numeric-strings.php" class="link">numeric string</a>,
     an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> is emitted.
    </span>
   </li>
  </ul>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.range-changelog">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.3.0</td>
      <td>
       If both <code class="parameter">start</code> and <code class="parameter">end</code>
       are strings then <span class="function"><strong>range()</strong></span> will now always produce
       an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> of bytes.
       Previously if one of the boundary values was a numeric string,
       then the other boundary value was implicitly cast to <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>.
      </td>
     </tr>

     <tr>
      <td>8.3.0</td>
      <td>
       An <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> is now emitted if
       <code class="parameter">start</code> or <code class="parameter">end</code>
       is a string that is implicitly cast to <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>
       because the other boundary value is a number.
      </td>
     </tr>

     <tr>
      <td>8.3.0</td>
      <td>
       An <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> is now emitted if
       <code class="parameter">start</code> or <code class="parameter">end</code>
       is a non-numeric string with more than one byte.
      </td>
     </tr>

     <tr>
      <td>8.3.0</td>
      <td>
       An <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> is now emitted if
       <code class="parameter">start</code> or <code class="parameter">end</code>
       is the empty string.
      </td>
     </tr>

     <tr>
      <td>8.3.0</td>
      <td>
       If <code class="parameter">step</code> is a <span class="type"><a href="language.types.float.php" class="type float">float</a></span> with no
       fractional part, it will be interpreted as an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>.
      </td>
     </tr>

     <tr>
      <td>8.3.0</td>
      <td>
       A <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> is now thrown if
       <code class="parameter">step</code> is negative when producing an increasing
       range.
      </td>
     </tr>

     <tr>
      <td>8.3.0</td>
      <td>
       A <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> is now thrown if
       <code class="parameter">step</code> is not finite.
      </td>
     </tr>

     <tr>
      <td>8.3.0</td>
      <td>
       A <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span> is now thrown if
       <code class="parameter">start</code> or <code class="parameter">end</code>
       is an <span class="type"><a href="language.types.array.php" class="type array">array</a></span>, <span class="type"><a href="language.types.object.php" class="type object">object</a></span>, or <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>.
       Previously they were implicitly cast to <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.range-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>range()</strong></span> examples</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">', '</span><span style="color: #007700">, </span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">12</span><span style="color: #007700">)), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">', '</span><span style="color: #007700">, </span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">100</span><span style="color: #007700">, </span><span style="color: #0000BB">10</span><span style="color: #007700">)), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">', '</span><span style="color: #007700">, </span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">, </span><span style="color: #DD0000">'i'</span><span style="color: #007700">)), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">', '</span><span style="color: #007700">, </span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #DD0000">'c'</span><span style="color: #007700">, </span><span style="color: #DD0000">'a'</span><span style="color: #007700">)), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">', '</span><span style="color: #007700">, </span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #DD0000">'A'</span><span style="color: #007700">, </span><span style="color: #DD0000">'z'</span><span style="color: #007700">)), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
a, b, c, d, e, f, g, h, i
c, b, a
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, [, \, ], ^, _, `, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.range-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.shuffle.php" class="function" rel="rdfs-seeAlso">shuffle()</a> - Shuffle an array</span></li>
    <li><span class="function"><a href="function.array-fill.php" class="function" rel="rdfs-seeAlso">array_fill()</a> - Fill an array with values</span></li>
    <li><a href="control-structures.foreach.php" class="link"><code class="literal">foreach</code></a></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>