PHP Program to Check Anagram with Example
strtolower to ignore case, then sort the characters of both strings with str_split and sort, and finally compare them with ==.Examples
How to Think About It
Algorithm
Code
<?php function areAnagrams(string $str1, string $str2): bool { $str1 = strtolower(str_replace(' ', '', $str1)); $str2 = strtolower(str_replace(' ', '', $str2)); $arr1 = str_split($str1); $arr2 = str_split($str2); sort($arr1); sort($arr2); return $arr1 == $arr2; } // Example usage $str1 = "listen"; $str2 = "silent"; if (areAnagrams($str1, $str2)) { echo "The strings are anagrams."; } else { echo "The strings are not anagrams."; }
Dry Run
Let's trace the example 'listen' and 'silent' through the code.
Convert to lowercase and remove spaces
'listen' -> 'listen', 'silent' -> 'silent'
Split strings into arrays
'listen' -> ['l','i','s','t','e','n'], 'silent' -> ['s','i','l','e','n','t']
Sort both arrays
['l','i','s','t','e','n'] -> ['e','i','l','n','s','t'], ['s','i','l','e','n','t'] -> ['e','i','l','n','s','t']
Compare sorted arrays
Both arrays are equal, so return true.
| Step | String 1 Array | String 2 Array |
|---|---|---|
| Initial split | ['l','i','s','t','e','n'] | ['s','i','l','e','n','t'] |
| After sort | ['e','i','l','n','s','t'] | ['e','i','l','n','s','t'] |
Why This Works
Step 1: Normalize strings
Using strtolower and str_replace makes sure case and spaces don't affect the comparison.
Step 2: Sort characters
Sorting the characters puts them in order so we can easily compare if both strings have the same letters.
Step 3: Compare arrays
If the sorted arrays are exactly the same, the strings are anagrams because they contain the same letters.
Alternative Approaches
<?php function areAnagramsCount(string $str1, string $str2): bool { $str1 = strtolower(str_replace(' ', '', $str1)); $str2 = strtolower(str_replace(' ', '', $str2)); $count1 = count_chars($str1, 1); $count2 = count_chars($str2, 1); return $count1 == $count2; } // Usage if (areAnagramsCount('Dormitory', 'Dirty room')) { echo "The strings are anagrams."; } else { echo "The strings are not anagrams."; }
<?php function areAnagramsMultibyte(string $str1, string $str2): bool { $str1 = mb_strtolower(str_replace(' ', '', $str1)); $str2 = mb_strtolower(str_replace(' ', '', $str2)); $arr1 = preg_split('//u', $str1, -1, PREG_SPLIT_NO_EMPTY); $arr2 = preg_split('//u', $str2, -1, PREG_SPLIT_NO_EMPTY); sort($arr1); sort($arr2); return $arr1 == $arr2; } // Usage if (areAnagramsMultibyte('résumé', 'sérumé')) { echo "The strings are anagrams."; } else { echo "The strings are not anagrams."; }
Complexity: O(n log n) time, O(n) space
Time Complexity
Sorting the characters dominates the time cost, which is O(n log n) where n is the string length.
Space Complexity
Extra space is used to store character arrays, so space complexity is O(n).
Which Approach is Fastest?
Counting characters with associative arrays can be faster (O(n)) but uses more memory; sorting is simpler and good for short strings.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Sorting characters | O(n log n) | O(n) | Simple and short strings |
| Counting characters | O(n) | O(n) | Long strings, performance sensitive |
| Multibyte sorting | O(n log n) | O(n) | Strings with special characters |