I'm trying to store all possible combinations of 4 letters from [Aa-Zz]. The problem is that I've got error 500
when I try to use the function below. When I try with a simple string it is working.
Here is the code that I have so far
function print_combinations($characters, $length, $combination = '') {
foreach ($characters as $i) {
print_combinations($characters, $length - 1, $combination . $i);
}
}
$characters = array_merge(range('A', 'Z'), range('a', 'z'));
$filename = 'test.txt';
$combos = print_combinations($characters, 4);
if (is_writable($filename)) {
if (!$fp = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($fp, $combos) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($combos) to file ($filename)";
fclose($fp);
} else {
echo "The file $filename is not writable";
}
If I change
$combos = print_combinations($characters, 4);
to
$combos = "some text;
it is working fine. So the problem is in how I call the function probably.
Any suggestions on this?