3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Returns the total number of $count-length strings generatable from $letters. function getPermCount($letters, $count) { $result = 1; // k characters from a set of n has n!/(n-k)! possible combinations for($i = strlen($letters) - $count + 1; $i <= strlen($letters); $i++) { $result *= $i; } return $result; } // Decodes $index to a $count-length string from $letters, no repeat chars. function getPerm($letters, $count, $index) { $result = array(); for($i = 0; $i < $count; $i++) { $pos = $index % strlen($letters); $result[] = $letters[$pos]; $index = ($index-$pos)/strlen($letters); $letters = substr($letters, 0, $pos) . substr($letters, $pos+1); } sort($result); return implode("", $result); } $r = array(); //$letters = 'abcdefghijklm'; $letters = 'abcdefghi'; $len = strlen($letters); for ($c = 1; $c <= $len; $c++) for($i = 0; $i < getPermCount($letters, $c); $i++) $r[] = getPerm($letters, $c, $i); print_r(array_unique($r, SORT_REGULAR));
Output for 7.0.31, 7.1.20 - 7.1.21, 7.2.8 - 7.2.9, 7.3.26, 7.3.32, 7.4.3, 7.4.12, 7.4.22, 8.0.0, 8.2.18, 8.3.5 - 8.3.6

Process exited with code 137.
Output for 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Fatal error: Out of memory (allocated 29364224 bytes) (tried to allocate 16777224 bytes) in /in/qvJTA on line 35 mmap() failed: [12] Cannot allocate memory mmap() failed: [12] Cannot allocate memory
Process exited with code 255.
Output for 7.2.29 - 7.2.33, 7.3.16 - 7.3.25, 7.3.27 - 7.3.31, 7.4.4 - 7.4.11, 7.4.13 - 7.4.21, 7.4.23 - 7.4.25, 7.4.27 - 7.4.33, 8.0.1 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.1, 8.1.28
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 33554440 bytes) in /in/qvJTA on line 35
Process exited with code 255.
Output for 8.1.2 - 8.1.27
Fatal error: Out of memory (allocated 18878464) (tried to allocate 16777224 bytes) in /in/qvJTA on line 35 mmap() failed: [12] Cannot allocate memory mmap() failed: [12] Cannot allocate memory
Process exited with code 255.
Output for 7.3.33, 7.4.26, 8.0.13
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 39456400 bytes) in /in/qvJTA on line 37
Process exited with code 255.

preferences:
129.97 ms | 401 KiB | 145 Q