mirror of
https://github.com/php/doc-en.git
synced 2026-03-24 07:42:10 +01:00
305 lines
9.3 KiB
XML
305 lines
9.3 KiB
XML
<?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
|
|
-->
|