Head Way
Head Way

Reputation: 323

Adding an additional character to the range function

range($low, $high, $step)

Create an array containing a range of elements

I would like to put my range as 1 - 12 but than add another option for 99. Can i do this by creating a variable and than passing the variable into the array?

<?php $infin = range(1,12,99); ?>

I'm not really sure how to accomplish this but it seems easy and i'm kind of drawing blanks right now.

Upvotes: 0

Views: 62

Answers (3)

Amir
Amir

Reputation: 4111

$infin=array_merge(range(1,12),array(99));

Upvotes: 0

George Cummins
George Cummins

Reputation: 28906

$infin = range(1, 12);
$infin[] = 99;

The second line allows you to add a new element to the end of an array.

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
    [10] => 11
    [11] => 12
    [12] => 99
)

Upvotes: 5

Barmar
Barmar

Reputation: 780974

$array = range(1, 12);
$array[] = 99;

Upvotes: 2

Related Questions