RE: [PHP] stepping through alphabet
| From: | Ford, Mike [LSS] | Date: | Wed, 19 Nov 2003 16:08:03 +0000 |
| Subject: | RE: [PHP] stepping through alphabet | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-170305@lists.php.net to get a copy of this message | ||
On 19 November 2003 14:25, Jay Blanchard contributed these pearls of wisdom:
> [snip]
> for ($letter = 'A'; $letter++; $letter <= 'Z')
> echo "<a
> href=\"?action=alphabet&letter=$letter\">$letter</a>\n";
> [/snip]
>
> I tested this and found two small problems. The code works if
> you do this (I was myself amazed that letters would increment)
Well, it's mentioned (briefly) in the manual
(http://uk.php.net/manual/en/language.operators.increment.php), so maybe you
shouldn't be so amazed.
>
> for($letter = 'A'; $letter < 'Z'; $letter++){
> echo $letter . "\n";
> }
>
> The problem seems to be that you cannot use <='Z' for some
> reason, as the array becomes quite lengthy, as if 'Z' doesn't
> work properly.
The problem is not that 'Z' doesn't work "properly", but that that
PHP's
idea of "properly" is different from yours: 'Z'+1 is defined to be
'AA' (it
says that in the manual, too!), so your loop has to be:
for($letter = 'A'; $letter <= 'AA'; $letter++){
echo $letter . "\n";
}
Cheers!
Mike
--
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211