greenboxgoolu
greenboxgoolu

Reputation: 139

PHP Range (Numbers and Alphabets)

<?php
$number = range(0,5);
print_r ($number);
?>

Above code work , but what if the data is 'ABC001' to 'ABC010' ? I tried with

$number = range('ABC001','ABC010');

it only return 'A' , any idea how do i do it ? or missing out anything ?

Upvotes: 1

Views: 223

Answers (2)

Syscall
Syscall

Reputation: 19780

Another way, using hexdec(), dechex() and array_map().

 $range = array_map('dechex', range(hexdec('ABC001'), hexdec('ABC010')));

Upvotes: 0

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146450

Arithmetic operations on character variables don't work with the range() function:

Character sequence values are limited to a length of one. If a length greater than one is entered, only the first character is used.

You need to roll your own code, e.g.:

$from = 'ABC001';
$to = 'ABC010';
$range = [];
for ($i = $from; $i <= $to; $i++) {
    $range[] = $i;
}
var_dump($range);

Upvotes: 3

Related Questions