Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - loop through letters

Tags:

I am trying to loop through letters rather than numbers.

I am trying to do this using chr and the number equivalent but it doesn't seem to be happening!

I want four letter loop.

So AAAA, AAAB, AAAC etc through to ZZZZ - and yes I know this will likely take a while to execute!

like image 714
user887515 Avatar asked Dec 02 '12 18:12

user887515


People also ask

Can I loop through a string in PHP?

To loop through words in a string in PHP, use explode() function with space as delimiter. The explode() functions returns an array containing words and you can then use a looping statement to traverse through the words one by one.

How do I print each letter of a string in PHP?

PHP has str_split () function, which is used to print every character of a string. No need to use any loop.

What are PHP loops?

Often when you write code, you want the same block of code to run over and over again a certain number of times. So, instead of adding several almost equal code-lines in a script, we can use loops. Loops are used to execute the same block of code again and again, as long as a certain condition is true.

How many main parameters are used in for loop?

How many main parameter are used in for loop? Explanation: There are three main parameters to the code, namely the initialization, the test condition and the counter.


2 Answers

Why don't you make an array of letters and then use nested loops:

$letters = range('A', 'Z');  foreach ($letters as $one) {   foreach ($letters as $two) {     foreach ($letters as $three) {       foreach ($letters as $four) {         echo "$one$two$three$four";       }     }   } } 
like image 59
Leigh Avatar answered Sep 30 '22 05:09

Leigh


for( $x = "AAAA"; ; $x++) {     echo $x."\n";     if( $x == "ZZZZ") break; } 

Incrementing a letter will cycle it through the alphabet similar to the column names in Excel.

like image 41
Niet the Dark Absol Avatar answered Sep 30 '22 04:09

Niet the Dark Absol