Search code examples
phparraysletter

How write all possible words in php?


Possible Duplicate:
Generate all combinations of arbitrary alphabet up to arbitrary length

I'm trying to make write all possible words of 10 letters(zzzzzzzzzz) in php. How can I do that? it will look like that : https://i.sstatic.net/xC381.png

I tried some ways to it but they are only making 10 letters randomly not increasing from 1 letter. By the way execution time and how it's big is not problem. i just need to algorithm for it, if somebody show it with code it'll be more helpful of course..


Solution

  • Version 1:

    for($s = 'a'; $s <= 'zzzzzzzzzz'; print $s++.PHP_EOL);
    

    as noted by Paul in comments below, this will only go to zzzzzzzzyz. A bit slower (if anyone cares) but correct version would be:

    //modified to include arnaud576875's method of checking exit condition
    for($s = 'a'; !isset($s[10]); print $s++.PHP_EOL);