print from A to Z

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xexexexe
Forum Newbie
Posts: 3
Joined: Mon Jul 15, 2002 6:47 am

print from A to Z

Post by xexexexe »

hi! i'm beginner at php... and i have a question: what should I do if I want that php print letters from A to Z?

:arrow: for ex. in turbo pascal it's written like this:
for c:= 'A' to 'Z' do
writeln(c);
samscripts
Forum Commoner
Posts: 57
Joined: Tue Apr 23, 2002 4:34 pm
Location: London, UK

Post by samscripts »

Several options:

Code: Select all

$letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

for( $i = 0; $i < 26; $i++) echo $letters&#1111;$i];
or:

Code: Select all

$A = ord("A");
$Z = $A + 25;
for( $i = $A; $i <= $Z; $i++) echo chr($i);
check out:
http://www.php.net/ord
http://www.php.net/chr
http://www.php.net/strings
http://www.php.net/for

hope this helps, Sam
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Another option is:

Code: Select all

<?php 
for ($i = 'A'; $i != 'AA'; $i++) &#123; 
    echo $i; 
&#125; 
?>
Mac
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post by gnu2php »

You could also do this:

Code: Select all

for ($x = 0; $x < 26; $x++)
&#123;
	print chr($x + ord('A'))."\n";
&#125;
Post Reply