act / cat => anagram: yes
listen / silent => anagram: yes
hello / world => anagram: no
How It Works
Build two maps. Mode 1 for each word.
Compare with ===. Same keys and counts means same multiset of bytes.
Order ignored.listen and silent rearrange the same letters.
Case tip. Use strtolower() first if you want case-insensitive anagrams.
Frequently Asked Questions
count_chars() counts how many times each byte value (0–255) appears in a string. Depending on the mode, it returns a full frequency array, a filtered array, or a string of unique or unused characters.
array|string count_chars(string $string, int $mode = 0). The $string is analyzed and $mode controls the return format. Mode 1 is the most commonly used for readable frequency maps.
Mode 0 returns all 256 byte frequencies. Mode 1 returns only bytes that appear at least once. Mode 2 returns only bytes with zero count. Mode 3 returns a string of unique characters. Mode 4 returns a string of unused byte characters.
Yes. Uppercase and lowercase letters are different bytes—ord('A') is 65 and ord('a') is 97—so they are counted separately.
No. count_chars() analyzes raw bytes, not Unicode characters. A multibyte UTF-8 character like é is counted as multiple separate byte entries. For Unicode-aware counting, iterate with mb_strlen() and mb_substr().