Increment char in for loop

时间:2015-06-26 09:48:59

标签: php

I would like to know how i can make something like this : Does a php function exists ?

for ($i=0; $i < 26; $i++) { 
    // echoes "A" ... b ... c ...
}

2 个答案:

答案 0 :(得分:1)

PHP supports Perl-style character incrementing

so simply use

for($char = 'A'; $char !== 'IW'; $char++) {
    echo $char, PHP_EOL;
}

which will print out from A to IV.... it is important to use a !== in the terminating comparison though

答案 1 :(得分:1)

You can also use range()

foreach (range('a','z') as $char)
{
    // do something with $char
}