3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * take the range of characters and generate a string of all permutations * * @param array $range range of characters to itterate over * @param array $array input array - operated on by reference * @param int $depth how many chars to put in the resultant array should be * @param int $currentDepth internal variable to track how nested the current call is * @param string $prefix internal variable to know what to prefix the current string with * @return string permutations */ function foo($range, &$array, $depth = 1, $currentDepth = 0, $prefix = "") { $start = !$currentDepth; $currentDepth++; if ($currentDepth > $depth) { return; } foreach($range as $char) { if ($currentDepth === $depth) { $array[] = $prefix . $char; continue; } foo($range, $array, $depth, $currentDepth, $prefix . $char); } if ($start) { return implode($array, "\n"); } } $return = array(); echo foo(range('a', 'z'), $return, 2);

preferences:
31.3 ms | 402 KiB | 5 Q