📄 function.range.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Create an array containing a range of elements</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.prev.html">prev</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.reset.html">reset</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.range" class="refentry"> <div class="refnamediv"> <h1 class="refname">range</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">range</span> — <span class="dc-title">Create an array containing a range of elements</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><b><b>range</b></b></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$low</tt></span> , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$high</tt></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.number" class="type number">number</a></span> <tt class="parameter">$step</tt></span> ] )</div> <p class="para rdfs-comment"> Create an array containing a range of elements. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">low</tt></i></span> <dd> <p class="para"> Low value. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">high</tt></i></span> <dd> <p class="para"> High value. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">step</tt></i></span> <dd> <p class="para"> If a <i><tt class="parameter">step</tt></i> value is given, it will be used as the increment between elements in the sequence. <i><tt class="parameter">step</tt></i> should be given as a positive number. If not specified, <i><tt class="parameter">step</tt></i> will default to 1. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns an array of elements from <i><tt class="parameter">low</tt></i> to <i><tt class="parameter">high</tt></i>, inclusive. If low > high, the sequence will be from high to low. </p> </div> <div class="refsect1 changelog"> <h3 class="title">ChangeLog</h3> <p class="para"> <table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Version</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">5.0.0</td> <td colspan="1" rowspan="1" align="left"> The optional <i><tt class="parameter">step</tt></i> parameter was added. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.1.0 to 4.3.2</td> <td colspan="1" rowspan="1" align="left"> In PHP versions 4.1.0 through 4.3.2, <b>range()</b> sees numeric strings as strings and not integers. Instead, they will be used for character sequences. For example, <i>"4242"</i> is treated as <i>"4"</i>. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.1.0</td> <td colspan="1" rowspan="1" align="left"> Prior to PHP 4.1.0, <b>range()</b> only generated incrementing integer arrays. Support for character sequences and decrementing arrays was added in 4.1.0. Character sequence values are limited to a length of one. If a length greater than one is entered, only the first character is used. </td> </tr> </tbody> </colgroup> </table> </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>range()</b> examples</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)<br /></span><span style="color: #007700">foreach (</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">) as </span><span style="color: #0000BB">$number</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">$number</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// The step parameter was introduced in 5.0.0<br />// array(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)<br /></span><span style="color: #007700">foreach (</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">) as </span><span style="color: #0000BB">$number</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">$number</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// Use of character sequences introduced in 4.1.0<br />// array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');<br /></span><span style="color: #007700">foreach (</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">) as </span><span style="color: #0000BB">$letter</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">$letter</span><span style="color: #007700">;<br />}<br /></span><span style="color: #FF8000">// array('c', 'b', 'a');<br /></span><span style="color: #007700">foreach (</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">) as </span><span style="color: #0000BB">$letter</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">$letter</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.shuffle.html" class="function" rel="rdfs-seeAlso">shuffle()</a></li> <li class="member"><a href="function.array-fill.html" class="function" rel="rdfs-seeAlso">array_fill()</a></li> <li class="member"><a href="control-structures.foreach.html" class="link">foreach</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.prev.html">prev</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.reset.html">reset</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -