0

i need to do a simple loop in php to get char from a to z.. something like:

for($i=0;$i<aNumber;$i++) echo "char = ".intToChar($i)."<br>"; 

where aNumber is less then 20

4 Answers 4

4

You can create an array of characters easily with range [docs]:

$chars = range('a', 'z'); 

Or if you really only want to print them:

echo implode('<br />', range('a', 'z')); 
Sign up to request clarification or add additional context in comments.

1 Comment

Range was very helpful. Thank you. Starting to like PHP more and more
2

You need understand ASCII table. See in this link. Codes for A - Z is 65 - 90 and a - z is 97 - 122.

for($i = 97; $i <= 122; $i++) echo chr($i); 

Comments

1

I SOLVED IN THIS WAY:

$i = 0; $char = 'a'; $aNumber = xx; while($i<$aNumber){ echo $char."<br>"; $char++; $i++; } 

1 Comment

You must try your best shot before making a question.
0

To return character specified by ASCII code/number use chr function. Since you want to loop from 0 to 20, then you need to add 97 to $i, 97 is ASCII code for letter a:

for($i=0;$i<$aNumber;$i++) echo "char = ".chr($i+97)."<br>"; // + 97 because it is ASCII 'a' 

If you just want to loop through those letters you can do it like this:

for($i = ord('a'); $i < ord('z'); $i++) echo "char = ".chr($i)."<br>"; 

Additionally if you just want to get array of those characters you can use range function:

$chars = range('a', 'z'); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.