RE: Letters in Loops

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> -----Original Message-----
> From: Ben Miller [mailto:benipmiller@xxxxxxxxxxx] 
> Sent: 23 March 2006 15:08
> To: php-general@xxxxxxxxxxxxx
> Subject:  Letters in Loops
> 
> 
> In trying to make an alpha list, using the following:
> 
> for($i=A;$i<=Z;$i++) {
> 	echo "$i";
> }
> 
> Produces:
> A
> B
> C...
> X
> Y
> Z
> AA
> AB
> AC...
> AX
> AY
> AZ... all the way to YZ.
> 
> 
> What am I doing wrong that it's not stopping at just plain 
> old "Z", without moving on to "AA" and continuing?

Well, think about it -- you have all the evidence in front of you!

If the iteration after "Z" is "AA" (which is as documented for the ++
operator on strings), when is the first time that $i<='Z' evaluates to
false, and hence your loop stops? It's when it reaches "ZA", which is,
not surprisingly, the iteration after "YZ".

So you need it to exit when you reach the value after "Z", which you can
see is "AA", thus you need to write:

  for($i='A'; $i!='AA'; $i++)

You might also consider the use of:

  foreach (range('A','Z') as $i)
Cheers!

Mike
 
------------------------------------------------------------------------
----------------
Mike Ford, Electronic Information Services Adviser, Learning Support
Services,
JG125, The Library, James Graham Building, Headingley Campus, Beckett
Park,
LEEDS, LS6 3QS,     United Kingdom
Tel: +44 113 283 2600 extn 4730    Fax: +44 113 283 3211


To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux