PHP range() from A to ZZ?

Take advantage of PHP’s ability to increment characters “perl-style”

$letters = array();
$letter="A";
while ($letter !== 'AAA') {
    $letters[] = $letter++;
}

But you could also use simple integer values, and take advantage of PHPExcel’s built-in PHPExcel_Cell::stringFromColumnIndex() method

EDIT

From PHP 5.5, you can also use Generators to avoid actually building the array in memory

function excelColumnRange($lower, $upper) {
    ++$upper;
    for ($i = $lower; $i !== $upper; ++$i) {
        yield $i;
    }
}

foreach (excelColumnRange('A', 'ZZ') as $value) {
    echo $value, PHP_EOL;
}

Leave a Comment