reference/array/functions/range.xml
62421b98a97fe576987878055e17cf755eb395af
...
...
@@ -1,5 +1,4 @@
1
1
<?xml version="1.0" encoding="utf-8"?>
2
-
<!-- $Revision$ -->
3
2
<refentry xml:id="function.range" xmlns="http://docbook.org/ns/docbook">
4
3
<refnamediv>
5
4
<refname>range</refname>
...
...
@@ -9,14 +8,26 @@
9
8
&reftitle.description;
10
9
<methodsynopsis>
11
10
<type>array</type><methodname>range</methodname>
12
-
<methodparam><type>mixed</type><parameter>start</parameter></methodparam>
13
-
<methodparam><type>mixed</type><parameter>end</parameter></methodparam>
14
-
<methodparam choice="opt"><type>number</type><parameter>step</parameter><initializer>1</initializer></methodparam>
11
+
<methodparam><type class="union"><type>string</type><type>int</type><type>float</type></type><parameter>start</parameter></methodparam>
12
+
<methodparam><type class="union"><type>string</type><type>int</type><type>float</type></type><parameter>end</parameter></methodparam>
13
+
<methodparam choice="opt"><type class="union"><type>int</type><type>float</type></type><parameter>step</parameter><initializer>1</initializer></methodparam>
15
14
</methodsynopsis>
16
15
<para>
17
16
Create an array containing a range of elements.
18
17
</para>
18
+
<para>
19
+
If both <parameter>start</parameter> and <parameter>end</parameter> are
20
+
<type>string</type>s, and <parameter>step</parameter> is <type>int</type>
21
+
the produced array will be a sequence of bytes.
22
+
Otherwise, the produced array will be a sequence of numbers.
23
+
</para>
24
+
<para>
25
+
The sequence is increasing if <parameter>start</parameter> is less than
26
+
equal to <parameter>end</parameter>.
27
+
Otherwise, the sequence is decreasing.
28
+
</para>
19
29
</refsect1>
30
+

20
31
<refsect1 role="parameters">
21
32
&reftitle.parameters;
22
33
<para>
...
...
@@ -33,8 +44,7 @@
33
44
<term><parameter>end</parameter></term>
34
45
<listitem>
35
46
<para>
36
-
The sequence is ended upon reaching the
37
-
<parameter>end</parameter> value.
47
+
Last possible value of the sequence.
38
48
</para>
39
49
</listitem>
40
50
</varlistentry>
...
...
@@ -42,23 +52,190 @@
42
52
<term><parameter>step</parameter></term>
43
53
<listitem>
44
54
<para>
45
-
If a <parameter>step</parameter> value is given, it will be used as the
46
-
increment between elements in the sequence. <parameter>step</parameter>
47
-
should be given as a positive number. If not specified,
48
-
<parameter>step</parameter> will default to 1.
55
+
<parameter>step</parameter> indicates by how much is the produced
56
+
sequence progressed between values of the sequence.
57
+
</para>
58
+
<para>
59
+
<parameter>step</parameter> may be negative for decreasing sequences.
60
+
</para>
61
+
<para>
62
+
If <parameter>step</parameter> is a <type>float</type> without a
63
+
fractional part, it is interpreted as <type>int</type>.
49
64
</para>
50
65
</listitem>
51
66
</varlistentry>
52
67
</variablelist>
53
68
</para>
54
69
</refsect1>
70
+

55
71
<refsect1 role="returnvalues">
56
72
&reftitle.returnvalues;
57
73
<para>
58
-
Returns an array of elements from <parameter>start</parameter> to
59
-
<parameter>end</parameter>, inclusive.
74
+
Returns a sequence of elements as an <type>array</type> with the first
75
+
element being <parameter>start</parameter> going up to
76
+
<parameter>end</parameter>, with each value of the sequence being
77
+
<parameter>step</parameter> values apart.
78
+
</para>
79
+
<para>
80
+
The last element of the returned array is either <parameter>end</parameter>
81
+
or the previous element of the sequence,
82
+
depending on the value of <parameter>step</parameter>.
83
+
</para>
84
+
<para>
85
+
If both <parameter>start</parameter> and <parameter>end</parameter> are
86
+
<type>string</type>s, and <parameter>step</parameter> is <type>int</type>
87
+
the produced array will be a sequence of bytes,
88
+
generally latin <abbrev>ASCII</abbrev> characters.
89
+
</para>
90
+
<para>
91
+
If at least one of <parameter>start</parameter>, <parameter>end</parameter>,
92
+
or <parameter>step</parameter> is <type>float</type>
93
+
the produced array will be a sequence of <type>float</type>.
60
94
</para>
95
+
<para>
96
+
Otherwise, the produced array will be a sequence of <type>int</type>.
97
+
</para>
98
+
</refsect1>
99
+

100
+
<refsect1 role="errors">
101
+
&reftitle.errors;
102
+
<itemizedlist>
103
+
<listitem>
104
+
<simpara>
105
+
If <parameter>step</parameter> is <literal>0</literal>,
106
+
a <classname>ValueError</classname> is thrown.
107
+
</simpara>
108
+
</listitem>
109
+
<listitem>
110
+
<simpara>
111
+
If <parameter>start</parameter>, <parameter>end</parameter>,
112
+
or <parameter>step</parameter> is not <function>is_finite</function>,
113
+
a <classname>ValueError</classname> is thrown.
114
+
</simpara>
115
+
</listitem>
116
+
<listitem>
117
+
<simpara>
118
+
If <parameter>step</parameter> is negative,
119
+
but the produced range is increasing
120
+
(i.e. <code>$start &lt;= $end</code>),
121
+
a <classname>ValueError</classname> is thrown.
122
+
</simpara>
123
+
</listitem>
124
+
<listitem>
125
+
<simpara>
126
+
If <parameter>start</parameter> or <parameter>end</parameter>
127
+
is the empty string <literal>''</literal>,
128
+
an <constant>E_WARNING</constant> is emitted and
129
+
the empty string will be interpreted as <literal>0</literal>.
130
+
</simpara>
131
+
</listitem>
132
+
<listitem>
133
+
<simpara>
134
+
If <parameter>start</parameter> or <parameter>end</parameter>
135
+
non-<link linkend="language.types.numeric-strings">numeric string</link>
136
+
with more than one byte, an <constant>E_WARNING</constant> is emitted.
137
+
</simpara>
138
+
</listitem>
139
+
<listitem>
140
+
<simpara>
141
+
If <parameter>start</parameter> or <parameter>end</parameter> is a string
142
+
that is implicitly cast to an <type>int</type> because the other boundary
143
+
value is a number, an <constant>E_WARNING</constant> is emitted.
144
+
</simpara>
145
+
</listitem>
146
+
<listitem>
147
+
<simpara>
148
+
If <parameter>step</parameter> is a <type>float</type>,
149
+
and <parameter>start</parameter> and <parameter>end</parameter> are
150
+
non-<link linkend="language.types.numeric-strings">numeric string</link>,
151
+
an <constant>E_WARNING</constant> is emitted.
152
+
</simpara>
153
+
</listitem>
154
+
</itemizedlist>
61
155
</refsect1>
156
+

157
+
<refsect1 role="changelog">
158
+
&reftitle.changelog;
159
+
<informaltable>
160
+
<tgroup cols="2">
161
+
<thead>
162
+
<row>
163
+
<entry>&Version;</entry>
164
+
<entry>&Description;</entry>
165
+
</row>
166
+
</thead>
167
+
<tbody>
168
+
<row>
169
+
<entry>8.3.0</entry>
170
+
<entry>
171
+
If both <parameter>start</parameter> and <parameter>end</parameter>
172
+
are strings then <function>range</function> will now always produce
173
+
an <type>array</type> of bytes.
174
+
Previously if one of the boundary values was a numeric string,
175
+
then the other boundary value was implicitly cast to <type>int</type>.
176
+
</entry>
177
+
</row>
178
+
<row>
179
+
<entry>8.3.0</entry>
180
+
<entry>
181
+
An <constant>E_WARNING</constant> is now emitted if
182
+
<parameter>start</parameter> or <parameter>end</parameter>
183
+
is a string that is implicitly cast to <type>int</type>
184
+
because the other boundary value is a number.
185
+
</entry>
186
+
</row>
187
+
<row>
188
+
<entry>8.3.0</entry>
189
+
<entry>
190
+
An <constant>E_WARNING</constant> is now emitted if
191
+
<parameter>start</parameter> or <parameter>end</parameter>
192
+
is a non-numeric string with more than one byte.
193
+
</entry>
194
+
</row>
195
+
<row>
196
+
<entry>8.3.0</entry>
197
+
<entry>
198
+
An <constant>E_WARNING</constant> is now emitted if
199
+
<parameter>start</parameter> or <parameter>end</parameter>
200
+
is the empty string.
201
+
</entry>
202
+
</row>
203
+
<row>
204
+
<entry>8.3.0</entry>
205
+
<entry>
206
+
If <parameter>step</parameter> is a <type>float</type> with no
207
+
fractional part, it will be interpreted as an <type>int</type>.
208
+
</entry>
209
+
</row>
210
+
<row>
211
+
<entry>8.3.0</entry>
212
+
<entry>
213
+
A <classname>ValueError</classname> is now thrown if
214
+
<parameter>step</parameter> is negative when producing an increasing
215
+
range.
216
+
</entry>
217
+
</row>
218
+
<row>
219
+
<entry>8.3.0</entry>
220
+
<entry>
221
+
A <classname>ValueError</classname> is now thrown if
222
+
<parameter>step</parameter> is not finite.
223
+
</entry>
224
+
</row>
225
+
<row>
226
+
<entry>8.3.0</entry>
227
+
<entry>
228
+
A <classname>TypeError</classname> is now thrown if
229
+
<parameter>start</parameter> or <parameter>end</parameter>
230
+
is an <type>array</type>, <type>object</type>, or <type>resource</type>.
231
+
Previously they were implicitly cast to <type>int</type>.
232
+
</entry>
233
+
</row>
234
+
</tbody>
235
+
</tgroup>
236
+
</informaltable>
237
+
</refsect1>
238
+

62
239
<refsect1 role="examples">
63
240
&reftitle.examples;
64
241
<para>
...
...
@@ -67,41 +244,32 @@
67
244
<programlisting role="php">
68
245
<![CDATA[
69
246
<?php
70
-
// array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
71
-
foreach (range(0, 12) as $number) {
72
-
echo $number;
73
-
}
247
+
echo implode(', ', range(0, 12)), PHP_EOL;
248
+

249
+
echo implode(', ', range(0, 100, 10)), PHP_EOL;
74
250

75
-
// The step parameter
76
-
// array(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
77
-
foreach (range(0, 100, 10) as $number) {
78
-
echo $number;
79
-
}
251
+
echo implode(', ', range('a', 'i')), PHP_EOL;
80
252

81
-
// Usage of character sequences
82
-
// array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');
83
-
foreach (range('a', 'i') as $letter) {
84
-
echo $letter;
85
-
}
86
-
// array('c', 'b', 'a');
87
-
foreach (range('c', 'a') as $letter) {
88
-
echo $letter;
89
-
}
253
+
echo implode(', ', range('c', 'a')), PHP_EOL;
254
+

255
+
echo implode(', ', range('A', 'z')), PHP_EOL;
90
256
?>
91
257
]]>
92
258
</programlisting>
259
+
&example.outputs;
260
+
<screen>
261
+
<![CDATA[
262
+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
263
+
0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
264
+
a, b, c, d, e, f, g, h, i
265
+
c, b, a
266
+
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
267
+
]]>
268
+
</screen>
93
269
</example>
94
270
</para>
95
271
</refsect1>
96
-
<refsect1 role="notes">
97
-
&reftitle.notes;
98
-
<note>
99
-
<para>
100
-
Character sequence values are limited to a length of one. If a length greater
101
-
than one is entered, only the first character is used.
102
-
</para>
103
-
</note>
104
-
</refsect1>
272
+

105
273
<refsect1 role="seealso">
106
274
&reftitle.seealso;
107
275
<para>
...
...
@@ -112,8 +280,8 @@ foreach (range('c', 'a') as $letter) {
112
280
</simplelist>
113
281
</para>
114
282
</refsect1>
115
-
</refentry>
116
283

284
+
</refentry>
117
285
<!-- Keep this comment at the end of the file
118
286
Local variables:
119
287
mode: sgml
120
288