Permutations Script Needs Optimization
I've approached it from a hint @hacatu said, that you are storing all the permutations in an array. I thought move this to a generator, although I think trying out Heap's algorithm could be beneficial too.

I'm only adding the function that has changes, the rest of the code stayed the same. When using generators, you don't need to build up a whole array, you get an Generator object which implements the Iterator interface. The execution of your code stops at the yield statement and only continue if next() is called ie in a foreach

function permutations($letters,$num)
{
    $last = str_repeat($letters[0],$num);
    // no result array
    while($last != str_repeat($this-&gt;lastchar($letters),$num))
    {
        yield $last; // &lt;-- one change
        $last = $this-&gt;char_add($letters,$last,$num-1);
    }

    yield $last; // &lt;-- another change
}


I've put these methods in a class called Permutation and used it like this:

$perm = new Permutation();
$perms = $perm-&gt;permutations('1234', 10); // $perms is a Generator object here

// I'm looping through the results, if needed you can build up an array here, but that will need a lot of memory as you already seen
foreach ($perms as $result) {
    printf("%s\n", $result);
}

printf("Max memory: %sK\n", number_format(memory_get_peak_usage(true)/1024));


When I run this code (as it doesn't do much just prints) it ends up printing me:

lots of permutations here
....
4444444444
Max memory: 2,048K


When I add all the permutations to an array, the memory usage jumps

lots of permutations here
....
4444444444
Max memory: 75,780K


I hope this helps.

Ps: If you are into JavaScript and redux have a look at redux-saga, it can also help understanding generators. If not, I hope the PHP docs will help you :)