PHP Program to print all permutations of a given string Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report A permutation also called an "arrangement number" or "order," is a rearrangement of the elements of an ordered list S into a one-to-one A permutation also called an "arrangement number" or "order," is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation. Source: Mathword(https://mathworld.wolfram.com/Permutation.html) Below are the permutations of string ABC. ABC ACB BAC BCA CBA CAB Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. Here is a solution that is used as a basis in backtracking. PHP <?php // PHP program to print all // permutations of a given string. /* Permutation function @param str string to calculate permutation for @param l starting index @param r end index */ function permute($str, $l, $r) { if ($l == $r) echo $str. "\n"; else { for ($i = $l; $i <= $r; $i++) { $str = swap($str, $l, $i); permute($str, $l + 1, $r); $str = swap($str, $l, $i); } } } /* Swap Characters at position @param a string value @param i position 1 @param j position 2 @return swapped string */ function swap($a, $i, $j) { $temp; $charArray = str_split($a); $temp = $charArray[$i] ; $charArray[$i] = $charArray[$j]; $charArray[$j] = $temp; return implode($charArray); } // Driver Code $str = "ABC"; $n = strlen($str); permute($str, 0, $n - 1); // This code is contributed by mits. ?> Output: ABC ACB BAC BCA CBA CAB Algorithm Paradigm: Backtracking Time Complexity: O(n*n!) Note that there are n! permutations and it requires O(n) time to print a permutation. Auxiliary Space: O(r - l) Note: The above solution prints duplicate permutations if there are repeating characters in the input string. Please see the below link for a solution that prints only distinct permutations even if there are duplicates in input.Print all distinct permutations of a given string with duplicates. Permutations of a given string using STL Comment More infoAdvertise with us Next Article How to Get All Substrings of a Given String in PHP ? K kartik Follow Improve Article Tags : Web Technologies PHP PHP Programs PHP-string Similar Reads PHP Program to Print All Duplicate Characters in a String This article will show you how to print duplicate characters in a string using PHP. Detecting and printing duplicate characters in a string is a common task in programming. In this article, we will explore various approaches to achieve this.Table of ContentUsing array_count_values() FunctionUsing As 3 min read PHP Program to Calculate the Permutation nPr This article will show you how to calculate the Permutation nPr using PHP. Permutations refer to the different ways in which a set of items can be arranged. In mathematics, the number of permutations of 'n' distinct items taken 'r' at a time is denoted as nPr. Calculating nPr, or permutations means 2 min read How to Get All Substrings of a Given String in PHP ? Extracting all possible substrings from a given string is a common task in text processing, pattern recognition, and various other applications in PHP programming. This operation involves generating every possible combination of characters present in the string, including both contiguous and non-con 3 min read PHP Program to Split & Join a String Given a String, the task is to split and join strings in PHP. It is a common operations that allow you to manipulate and manage text data effectively. Below are the approaches to split and join a string in PHP: Table of Content Using explode() and implode() FunctionUsing str_split() and Custom Join 3 min read PHP Program to Find Missing Characters to Make a String Pangram In this article, we will see how to find the missing characters to make the string Pangram using PHP. A pangram is a sentence containing every letter in the English Alphabet. Examples: Input: The quick brown fox jumps over the dogOutput: Missing Characters - alyzInput: The quick brown fox jumpsOutpu 2 min read Split a String into an Array of Words in PHP In PHP, splitting a string into an array involves breaking a string into smaller parts based on a delimiter or a pattern and storing those parts as elements of an array. PHP provides several methods to achieve this, making it easy to convert a string into an array of words:Table of ContentUsing expl 3 min read Like