W3docs

count_chars()

The count_chars() function is used to count the number of occurrences of each character in a string. The syntax of the count_chars() function is as follows:

The PHP count_chars() function reports, for every possible byte value (0 to 255), how many times it appears in a string. Instead of looking at words or characters one at a time, it gives you a frequency snapshot of the whole string in a single call. This makes it handy for tasks like building character histograms, detecting which characters a string uses, or quickly spotting unexpected bytes in input.

This page covers the syntax, every $mode and exactly what each one returns, a worked example, and the common gotchas (such as the ASCII keys and how it handles multibyte text).

Syntax

count_chars(string $string, int $mode = 0): array|string
  • $string — the string to analyze.
  • $mode — controls the shape of the result (default 0). See the modes below.

The keys returned by count_chars() are byte values (0–255), not the characters themselves. For example the byte 108 is the letter l. Use chr() to turn a byte value back into its character, and ord() to go the other way.

The five modes

ModeReturns
0An array for all 256 byte values, even ones that never appear (count 0).
1An array of only the bytes that appear at least once, keyed by byte value.
2An array of only the bytes that never appear (count 0).
3A string containing each distinct byte that was used, in ascending order.
4A string containing each byte that was not used.

Modes 0, 1, and 2 return an array; modes 3 and 4 return a string.

Basic example

Mode 1 is the one you reach for most often — it lists just the characters that actually occur and how many times each does.

php— editable, runs on the server

The output is:

Array
(
    [32] => 1
    [33] => 1
    [44] => 1
    [72] => 1
    [87] => 1
    [100] => 1
    [101] => 1
    [108] => 3
    [111] => 2
    [114] => 1
)

Each key is a byte value and each value is its count. So [108] => 3 means the byte 108 (the letter l) appears three times, and [111] => 2 means o appears twice. The space (32), comma (44) and exclamation mark (33) are counted too.

Getting readable output with chr()

Because the keys are byte values, raw output is hard to read. Convert each key back to its character with chr():

<?php
$string = "Hello, World!";
foreach (count_chars($string, 1) as $byte => $times) {
    printf("'%s' (byte %d) appears %d time(s)\n", chr($byte), $byte, $times);
}
?>

Output:

' ' (byte 32) appears 1 time(s)
'!' (byte 33) appears 1 time(s)
',' (byte 44) appears 1 time(s)
'H' (byte 72) appears 1 time(s)
'W' (byte 87) appears 1 time(s)
'd' (byte 100) appears 1 time(s)
'e' (byte 101) appears 1 time(s)
'l' (byte 108) appears 3 time(s)
'o' (byte 111) appears 2 time(s)
'r' (byte 114) appears 1 time(s)

Modes 3 and 4: which characters are (not) used

When you only care about which characters appear — not how many times — modes 3 and 4 return a compact string instead of an array.

<?php
$string = "Hello, World!";

echo count_chars($string, 3) . "\n"; //  !,HWdelor
echo strlen(count_chars($string, 3)) . "\n"; // 10  (10 distinct bytes used)
echo strlen(count_chars($string, 4)) . "\n"; // 246 (256 - 10 = bytes never used)
?>

Mode 3 is a quick way to get the set of distinct characters in a string, already sorted.

Common gotchas

  • Keys are bytes, not characters. Always remember count_chars() indexes by byte value 0–255. Pair it with chr() for human-readable output.
  • It is not multibyte-aware. A UTF-8 character such as é is two bytes, and count_chars() will count each byte separately rather than the character as a whole. For Unicode text, prefer mb_strlen() / preg_match_all() instead.
  • Mode 0 is large. It always returns 256 entries (most with count 0), so use mode 1 when you only want the characters that appear.
  • strlen() — total length of a string in bytes.
  • substr_count() — count occurrences of a substring (not a single byte).
  • str_word_count() — count words rather than characters.
  • str_split() — split a string into an array of characters.
  • ord() and chr() — convert between a character and its byte value.

Practice

Practice
What does the PHP 'count_chars()' function do?
What does the PHP 'count_chars()' function do?
Was this page helpful?