I was trying to loop though a-z with a do while loop.
I know that I also can do that with foreach and forloop.
我试着用do while循环遍历a-z。我知道我也可以对foreach和forloop这样做。
$char = 'a';
do {
echo $char;
$char++;
} while ($char <= 'z');
Why is that giving the output:
为什么输出是:
abcdefghijklmnopqrstuvwxyzaaabacadaeafagahaiajakalamanaoapaqarasatauavawaxayazbabbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbwbxbybzcacbcccdcecfcgchcicjckclcmcncocpcqcrcsctcucvcwcxcyczdadbdcdddedfdgdhdidjdkdldmdndodpdqdrdsdtdudvdwdxdydzeaebecedeeefegeheiejekelemeneoepeqereseteuevewexeyezfafbfcfdfefffgfhfifjfkflfmfnfofpfqfrfsftfufvfwfxfyfzgagbgcgdgegfggghgigjgkglgmgngogpgqgrgsgtgugvgwgxgygzhahbhchdhehfhghhhihjhkhlhmhnhohphqhrhshthuhvhwhxhyhziaibicidieifigihiiijikiliminioipiqirisitiuiviwixiyizjajbjcjdjejfjgjhjijjjkjljmjnjojpjqjrjsjtjujvjwjxjyjzkakbkckdkekfkgkhkikjkkklkmknkokpkqkrksktkukvkwkxkykzlalblcldlelflglhliljlklllmlnlolplqlrlsltlulvlwlxlylzmambmcmdmemfmgmhmimjmkmlmmmnmompmqmrmsmtmumvmwmxmymznanbncndnenfngnhninjnknlnmnnnonpnqnrnsntnunvnwnxnynzoaobocodoeofogohoiojokolomonooopoqorosotouovowoxoyozpapbpcpdpepfpgphpipjpkplpmpnpopppqprpsptpupvpwpxpypzqaqbqcqdqeqfqgqhqiqjqkqlqmqnqoqpqqqrqsqtquqvqwqxqyqzrarbrcrdrerfrgrhrirjrkrlrmrnrorprqrrrsrtrurvrwrxryrzsasbscsdsesfsgshsisjskslsmsnsospsqsrssstsusvswsxsysztatbtctdtetftgthtitjtktltmtntotptqtrtstttutvtwtxtytzuaubucudueufuguhuiujukulumunuoupuqurusutuuuvuwuxuyuzvavbvcvdvevfvgvhvivjvkvlvmvnvovpvqvrvsvtvuvvvwvxvyvzwawbwcwdwewfwgwhwiwjwkwlwmwnwowpwqwrwswtwuwvwwwxwywzxaxbxcxdxexfxgxhxixjxkxlxmxnxoxpxqxrxsxtxuxvxwxxxyxzyaybycydyeyfygyhyiyjykylymynyoypyqyrysytyuyvywyxyyyz
instead of just:
而不是:
abcdefghijklmnopqrstuvwxyz
3 个解决方案
#1
8
From the documentation:
从文档:
PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's. For example, in PHP and Perl
$a = 'Z'; $a++;
turns$a
into'AA'
, while in Ca = 'Z'; a++;
turns a into'['
(ASCII value of'Z'
is 90, ASCII value of'['
is 91). Note that character variables can be incremented but not decremented and even so only plain ASCII alphabets and digits (a-z, A-Z and 0-9) are supported.PHP在处理字符变量而不是C的算术操作时遵循Perl的惯例。例如,在PHP和Perl中,$a = 'Z';$ + +;将$a转换为AA, C a = 'Z';+ +;将a转换为“[”(ASCII值为“Z”为90,ASCII值为“[”]为91)。注意,字符变量可以递增,但不能递减,甚至只支持普通的ASCII字母和数字(a-z、a-z和0-9)。
Try something like this:
试试这样:
for($i = 0, $char = 'a'; $i < 26; $i++, $char++) {
echo $char;
}
#2
4
Because
因为
<?php
$char = 'z';
var_dump(++$char); //string(2) "aa"
var_dump('aa' <= 'z'); //bool(true)
var_dump('za' <= 'z'); //bool(false)
演示
Personally I'd just use a loop from 97 (ascii value for a
) to 122 (ascii value for z
):
就我个人而言,我只是使用一个循环从97 (ascii值为a)到122 (z的ascii值):
for ($i = 97; $i <= 122; $i++) {
echo chr($i);
}
#3
4
You cannot compare z
with 26
or some kind of number. You need something to compare it with the number. The function ord()
does it. So, you can do something like:
你不能将z与26或某种数字进行比较。你需要一些东西来和数字进行比较。函数ord()做到了这一点。你可以这样做:
$char = 'a';
do {
echo $char;
$char++;
} while (ord($char) <= ord('z'));
#1
8
From the documentation:
从文档:
PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's. For example, in PHP and Perl
$a = 'Z'; $a++;
turns$a
into'AA'
, while in Ca = 'Z'; a++;
turns a into'['
(ASCII value of'Z'
is 90, ASCII value of'['
is 91). Note that character variables can be incremented but not decremented and even so only plain ASCII alphabets and digits (a-z, A-Z and 0-9) are supported.PHP在处理字符变量而不是C的算术操作时遵循Perl的惯例。例如,在PHP和Perl中,$a = 'Z';$ + +;将$a转换为AA, C a = 'Z';+ +;将a转换为“[”(ASCII值为“Z”为90,ASCII值为“[”]为91)。注意,字符变量可以递增,但不能递减,甚至只支持普通的ASCII字母和数字(a-z、a-z和0-9)。
Try something like this:
试试这样:
for($i = 0, $char = 'a'; $i < 26; $i++, $char++) {
echo $char;
}
#2
4
Because
因为
<?php
$char = 'z';
var_dump(++$char); //string(2) "aa"
var_dump('aa' <= 'z'); //bool(true)
var_dump('za' <= 'z'); //bool(false)
演示
Personally I'd just use a loop from 97 (ascii value for a
) to 122 (ascii value for z
):
就我个人而言,我只是使用一个循环从97 (ascii值为a)到122 (z的ascii值):
for ($i = 97; $i <= 122; $i++) {
echo chr($i);
}
#3
4
You cannot compare z
with 26
or some kind of number. You need something to compare it with the number. The function ord()
does it. So, you can do something like:
你不能将z与26或某种数字进行比较。你需要一些东西来和数字进行比较。函数ord()做到了这一点。你可以这样做:
$char = 'a';
do {
echo $char;
$char++;
} while (ord($char) <= ord('z'));