1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
<?xml version="1.0" encoding="utf-8"?>
<refentry xml:id="function.range" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>range</refname>
<refpurpose>Create an array containing a range of elements</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>array</type><methodname>range</methodname>
<methodparam><type class="union"><type>string</type><type>int</type><type>float</type></type><parameter>start</parameter></methodparam>
<methodparam><type class="union"><type>string</type><type>int</type><type>float</type></type><parameter>end</parameter></methodparam>
<methodparam choice="opt"><type class="union"><type>int</type><type>float</type></type><parameter>step</parameter><initializer>1</initializer></methodparam>
</methodsynopsis>
<para>
Create an array containing a range of elements.
</para>
<para>
If both <parameter>start</parameter> and <parameter>end</parameter> are
<type>string</type>s, and <parameter>step</parameter> is <type>int</type>
the produced array will be a sequence of bytes.
Otherwise, the produced array will be a sequence of numbers.
</para>
<para>
The sequence is increasing if <parameter>start</parameter> is less than
equal to <parameter>end</parameter>.
Otherwise, the sequence is decreasing.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>start</parameter></term>
<listitem>
<para>
First value of the sequence.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>end</parameter></term>
<listitem>
<para>
Last possible value of the sequence.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>step</parameter></term>
<listitem>
<para>
<parameter>step</parameter> indicates by how much is the produced
sequence progressed between values of the sequence.
</para>
<para>
<parameter>step</parameter> may be negative for decreasing sequences.
</para>
<para>
If <parameter>step</parameter> is a <type>float</type> without a
fractional part, it is interpreted as <type>int</type>.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns a sequence of elements as an <type>array</type> with the first
element being <parameter>start</parameter> going up to
<parameter>end</parameter>, with each value of the sequence being
<parameter>step</parameter> values apart.
</para>
<para>
The last element of the returned array is either <parameter>end</parameter>
or the previous element of the sequence,
depending on the value of <parameter>step</parameter>.
</para>
<para>
If both <parameter>start</parameter> and <parameter>end</parameter> are
<type>string</type>s, and <parameter>step</parameter> is <type>int</type>
the produced array will be a sequence of bytes,
generally latin <abbrev>ASCII</abbrev> characters.
</para>
<para>
If at least one of <parameter>start</parameter>, <parameter>end</parameter>,
or <parameter>step</parameter> is <type>float</type>
the produced array will be a sequence of <type>float</type>.
</para>
<para>
Otherwise, the produced array will be a sequence of <type>int</type>.
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<itemizedlist>
<listitem>
<simpara>
If <parameter>step</parameter> is <literal>0</literal>,
a <classname>ValueError</classname> is thrown.
</simpara>
</listitem>
<listitem>
<simpara>
If <parameter>start</parameter>, <parameter>end</parameter>,
or <parameter>step</parameter> is not <function>is_finite</function>,
a <classname>ValueError</classname> is thrown.
</simpara>
</listitem>
<listitem>
<simpara>
If <parameter>step</parameter> is negative,
but the produced range is increasing
(i.e. <code>$start <= $end</code>),
a <classname>ValueError</classname> is thrown.
</simpara>
</listitem>
<listitem>
<simpara>
If <parameter>start</parameter> or <parameter>end</parameter>
is the empty string <literal>''</literal>,
an <constant>E_WARNING</constant> is emitted and
the empty string will be interpreted as <literal>0</literal>.
</simpara>
</listitem>
<listitem>
<simpara>
If <parameter>start</parameter> or <parameter>end</parameter> is a
non-<link linkend="language.types.numeric-strings">numeric string</link>
with more than one byte, an <constant>E_WARNING</constant> is emitted.
</simpara>
</listitem>
<listitem>
<simpara>
If <parameter>start</parameter> or <parameter>end</parameter> is a string
that is implicitly cast to an <type>int</type> because the other boundary
value is a number, an <constant>E_WARNING</constant> is emitted.
</simpara>
</listitem>
<listitem>
<simpara>
If <parameter>step</parameter> is a <type>float</type>,
and <parameter>start</parameter> and <parameter>end</parameter> are
non-<link linkend="language.types.numeric-strings">numeric string</link>,
an <constant>E_WARNING</constant> is emitted.
</simpara>
</listitem>
</itemizedlist>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>8.3.0</entry>
<entry>
If both <parameter>start</parameter> and <parameter>end</parameter>
are strings then <function>range</function> will now always produce
an <type>array</type> of bytes.
Previously if one of the boundary values was a numeric string,
then the other boundary value was implicitly cast to <type>int</type>.
</entry>
</row>
<row>
<entry>8.3.0</entry>
<entry>
An <constant>E_WARNING</constant> is now emitted if
<parameter>start</parameter> or <parameter>end</parameter>
is a string that is implicitly cast to <type>int</type>
because the other boundary value is a number.
</entry>
</row>
<row>
<entry>8.3.0</entry>
<entry>
An <constant>E_WARNING</constant> is now emitted if
<parameter>start</parameter> or <parameter>end</parameter>
is a non-numeric string with more than one byte.
</entry>
</row>
<row>
<entry>8.3.0</entry>
<entry>
An <constant>E_WARNING</constant> is now emitted if
<parameter>start</parameter> or <parameter>end</parameter>
is the empty string.
</entry>
</row>
<row>
<entry>8.3.0</entry>
<entry>
If <parameter>step</parameter> is a <type>float</type> with no
fractional part, it will be interpreted as an <type>int</type>.
</entry>
</row>
<row>
<entry>8.3.0</entry>
<entry>
A <classname>ValueError</classname> is now thrown if
<parameter>step</parameter> is negative when producing an increasing
range.
</entry>
</row>
<row>
<entry>8.3.0</entry>
<entry>
A <classname>ValueError</classname> is now thrown if
<parameter>step</parameter> is not finite.
</entry>
</row>
<row>
<entry>8.3.0</entry>
<entry>
A <classname>TypeError</classname> is now thrown if
<parameter>start</parameter> or <parameter>end</parameter>
is an <type>array</type>, <type>object</type>, or <type>resource</type>.
Previously they were implicitly cast to <type>int</type>.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>range</function> examples</title>
<programlisting role="php">
<![CDATA[
<?php
echo implode(', ', range(0, 12)), PHP_EOL;
echo implode(', ', range(0, 100, 10)), PHP_EOL;
echo implode(', ', range('a', 'i')), PHP_EOL;
echo implode(', ', range('c', 'a')), PHP_EOL;
echo implode(', ', range('A', 'z')), PHP_EOL;
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
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
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>shuffle</function></member>
<member><function>array_fill</function></member>
<member>&foreach;</member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|