range
CREATE an ARRAY containing a range of elements.
$step if given, it will be used as the increment between elements in the sequence.
$step must be a positive number; the default is $step = 1
<?php
arr range ( mix $start , mix $end [, nbr $step = 1 ] )
where,
$start = The first value of the sequence
$end = The end value of the sequence
$step = To establish a constant variation ratio for
all elements belonging to given interval
?>
$start
The FIRST value of the sequence.
$end
The END value of the sequence.
$step
Used to establish a constant variation ratio for all elements belonging to given interval.
EXERCISE
<?php
$startval01 = 4;
$endval01 = 23;
$step01 = 2;
echo "Starting value: $startval01
<br><br>Final value: $endval01
<br><br>Step: $step01<br><br>";
$arr01 = range($startval01, $endval01, $step01);
foreach($arr01 as $a01 => $ar01)
{
echo '{' . $a01 . '] => ' . $ar01 . '<br>';
}
?>
RESULT
Starting value: 4
Final value: 23
Step: 2
[0] => 4
[1] => 6
[2] => 8
[3] => 10
[4] => 12
[5] => 14
[6] => 16
[7] => 18
[8] => 20
[9] => 22
EXERCISE
<?php
$startval02 = 23;
$endval02 = 4;
$step02 = -2;
echo "Starting value: $startval02
<br><br>Final value: $endval02
<br><br>Step: $step02<br><br>";
$arr02 = range($startval02, $endval02, $step02);
foreach($arr02 as $a02 => $ar02)
{
echo '{' . $a02 . '] => ' . $ar02 . '<br>';
}
?>
RESULT
Starting value: 23
Final value: 4
Step: -2
{0] => 23
{1] => 21
{2] => 19
{3] => 17
{4] => 15
{5] => 13
{6] => 11
{7] => 9
{8] => 7
{9] => 5
EXERCISE
<?php
$startval03 = 'p';
$endval03 = 'z';
$step03 = 1;
echo "Starting value: $startval03
<br><br>Final value: $endval03
<br><br>Step: $step03<br><br>";
$arr03 = range($startval03, $endval03, $step03);
foreach($arr03 as $a03 => $ar03)
{
echo '{' . $a03 . '] => ' . $ar03 . '<br>';
}
?>
RESULT
Starting value: p
Final value: z
Step: 1
[0] => p
[1] => q
[2] => r
[3] => s
[4] => t
[5] => u
[6] => v
[7] => w
[8] => x
[9] => y
[10] => z
EXERCISE
<?php
$startval04 = 'love';
$endval04 = 'LOVE';
$step04 = 2;
$arr04 = range($startval04, $endval04, $step04);
foreach($arr04 as $a04 => $ar04)
{
echo '{' . $a04 . '] => ' . $ar04 . '<br>';
}
?>
EXERCISE
<?php
$startval05 = '100';
$endval05 = 'GG';
$step05 = 10;
$arr05 = range($startval05, $endval05, $step05);
foreach($arr05 as $a05 => $ar05)
{
echo '{' . $a05 . '] => ' . $ar05 . '<br>';
}
?>
EXERCISE
<?php
$startval06 = 'sator arepo tenet opera rotas';
$endval06 = 'SATOR AREPO TENET OPERA ROTAS';
$step06 = 3;
$arr06 = range($startval06, $endval06, $step06);
foreach($arr06 as $a06 => $ar06)
{
echo '{' . $a06 . '] => ' . $ar06 . '<br>';
}
?>