2002-05-12 08:19:28 +00:00
|
|
|
<?xml version="1.0" encoding="iso-8859-1"?>
|
2003-12-15 16:55:22 +00:00
|
|
|
<!-- $Revision: 1.12 $ -->
|
2002-04-15 00:12:54 +00:00
|
|
|
<!-- splitted from ./en/functions/array.xml, last change in rev 1.2 -->
|
|
|
|
<refentry id="function.range">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>range</refname>
|
|
|
|
<refpurpose>
|
|
|
|
Create an array containing a range of elements
|
|
|
|
</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
2003-02-04 17:19:32 +00:00
|
|
|
<title>Description</title>
|
|
|
|
<methodsynopsis>
|
|
|
|
<type>array</type><methodname>range</methodname>
|
|
|
|
<methodparam><type>int</type><parameter>low</parameter></methodparam>
|
|
|
|
<methodparam><type>int</type><parameter>high</parameter></methodparam>
|
|
|
|
<methodparam choice="opt"><type>int</type><parameter>step</parameter></methodparam>
|
|
|
|
</methodsynopsis>
|
2002-04-15 00:12:54 +00:00
|
|
|
<para>
|
|
|
|
<function>range</function> returns an array of elements from
|
|
|
|
<parameter>low</parameter> to <parameter>high</parameter>,
|
|
|
|
inclusive. If low > high, the sequence will be from high to low.
|
|
|
|
</para>
|
2002-12-26 16:52:16 +00:00
|
|
|
<note>
|
|
|
|
<title>New parameter</title>
|
|
|
|
<simpara>
|
2003-01-24 12:58:51 +00:00
|
|
|
The optional <parameter>step</parameter> parameter was added in 5.0.0.
|
2002-12-26 16:52:16 +00:00
|
|
|
</simpara>
|
|
|
|
</note>
|
2002-11-14 05:57:58 +00:00
|
|
|
<para>
|
|
|
|
If a <parameter>step</parameter> value is given, it will be used as the
|
|
|
|
increment between elements in the sequence. <parameter>step</parameter>
|
|
|
|
should be given as a positive number. If not specified,
|
|
|
|
<parameter>step</parameter> will default to 1.
|
|
|
|
</para>
|
2002-12-26 16:52:16 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title><function>range</function> examples</title>
|
|
|
|
<programlisting role="php">
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
2002-12-26 16:52:16 +00:00
|
|
|
<?php
|
2003-08-17 12:21:03 +00:00
|
|
|
// array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,11, 12)
|
2003-12-15 16:55:22 +00:00
|
|
|
foreach (range(0, 12) as $number) {
|
2002-04-15 00:12:54 +00:00
|
|
|
echo $number;
|
|
|
|
}
|
2002-12-26 16:52:16 +00:00
|
|
|
|
2003-01-24 12:58:51 +00:00
|
|
|
// The step parameter was introduced in 5.0.0
|
2003-08-17 12:21:03 +00:00
|
|
|
// array(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
|
2003-12-15 16:55:22 +00:00
|
|
|
foreach (range(0, 100, 10) as $number) {
|
2002-11-14 05:57:58 +00:00
|
|
|
echo $number;
|
|
|
|
}
|
2002-12-26 16:52:16 +00:00
|
|
|
|
2003-06-22 23:17:14 +00:00
|
|
|
// Use of character sequences introduced in 4.1.0
|
2003-08-17 12:21:03 +00:00
|
|
|
// array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');
|
2003-12-15 16:55:22 +00:00
|
|
|
foreach (range('a', 'i') as $letter) {
|
2002-04-15 00:12:54 +00:00
|
|
|
echo $letter;
|
|
|
|
}
|
2003-08-17 12:21:03 +00:00
|
|
|
// array('c', 'b', 'a');
|
2003-12-15 16:55:22 +00:00
|
|
|
foreach (range('c', 'a') as $letter) {
|
2002-04-15 00:12:54 +00:00
|
|
|
echo $letter;
|
|
|
|
}
|
2002-12-26 16:52:16 +00:00
|
|
|
?>
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
2002-12-26 16:52:16 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
2002-04-15 00:12:54 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
2003-06-22 23:17:14 +00:00
|
|
|
Prior to PHP version 4.1.0, <function>range</function> 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.
|
2002-04-15 00:12:54 +00:00
|
|
|
</para>
|
|
|
|
</note>
|
2003-06-22 23:17:14 +00:00
|
|
|
<caution>
|
|
|
|
<para>
|
|
|
|
In PHP versions 4.1.0 through 4.3.2, <function>range</function> sees
|
|
|
|
numeric strings as strings and not integers. Instead, they will be
|
|
|
|
used for character sequences. For example, <literal>"4242"</literal>
|
|
|
|
is treated as <literal>"4"</literal>.
|
|
|
|
</para>
|
|
|
|
</caution>
|
2002-04-15 00:12:54 +00:00
|
|
|
<para>
|
2003-07-25 16:38:56 +00:00
|
|
|
See also <function>shuffle</function>,
|
|
|
|
<function>array_fill</function>, and
|
2002-12-26 16:52:16 +00:00
|
|
|
<link linkend="control-structures.foreach">foreach</link>.
|
2002-04-15 00:12:54 +00:00
|
|
|
</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:"../../../../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
|
|
|
|
-->
|