function permutations_of_words($items, $perms = array()) {
if (empty($items)) {
echo implode(' ', $perms) . "\n";
} else {
for ($i = 0; $i < count($items); $i++) {
$newItems = $items;
$newPerms = $perms;
list($foo) = array_splice($newItems, $i, 1);
array_unshift($newPerms, $foo);
permutations_of_words($newItems, $newPerms);
}
}
}
$words = array("PHP", "scripting", "language");
permutations_of_words($words);
/*
run:
language scripting PHP
scripting language PHP
language PHP scripting
PHP language scripting
scripting PHP language
PHP scripting language
*/