Re: Letters loop

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

 



MIGUEL ANTONIO GUIRAO AGUILAR wrote:

Hi!!

I wanna a do a for loop with letters, Is this possible?

for ($i = 'A'; $i <= 'Z'; $i++){
// code
}

--------------------------
MIGUEL GUIRAO AGUILERA
Logistica R8 - Telcel
Tel: (999) 960.7994
Cel: 9931-60000



No, but there are multiple ways this can be done.

You could create a string, loop through it's length, then access the $i character in the string.

$sAlpha = 'ABCDEF...XYZ';
for($i=0;$i<26;++$i)
{
   $sLetter = $sAlpha{$i};
   // code
}


or maybe loop through an array of the letters

foreach(array('A','B','C',...,'X','Y','Z') as $sLetter)
{
   // code
}

or even use the ASCII values

for($i=65;$i<91;++$i)
{
   $sLetter = chr($i);
   // code
}


I tend to prefer the first method, but, depending on your situation, one of the others might work better.


Chris

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


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux