PHP count_chars() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, count_chars() inspects which byte values appear in a string and how often.

The string is required. Mode is optional and changes whether you get counts, a list of used bytes, or unused bytes.

Syntax

Call count_chars with a mode. Start with this line.

PHP
$counts = count_chars($text, 1);
CallWhat it does
count_chars($text, 1)Array of bytes that appear, with counts.
count_chars($text, 3)String of unique used bytes.

Modes 0–4 change the return shape. Mode 1 is common for frequency maps.

Library declarations
PHP
count_chars(string $string, int $mode = 0): array|string

Parameters

ParameterDescription
stringRequired. The input string to analyze.
modeOptional. Return style (0–4). Default 0.

Return Value

Return ValueDescription
array|stringCounts or a byte list, depending on mode.

PHP Examples

Example 1 — Frequency Map (Mode 1)

Mode 1 returns only bytes that appear—easiest to read. Print keys with chr().

PHP
<?php
$string = "CodeToFun";
$counts = count_chars($string, 1);

foreach ($counts as $byte => $count) {
    echo chr($byte) . " ($byte) => $count\n";
}
?>

How It Works

  1. Analyze bytes. Each character becomes its byte value.
  2. Keep non-zeros. Mode 1 drops unused bytes 0–255.
  3. Read the map. 111 => 2 means o appears twice.
  4. Print labels. chr($byte) turns the key into a visible character.

Example 2 — Unique Characters (Mode 3)

Mode 3 returns a string of every distinct byte, sorted by code—not by first appearance.

PHP
<?php
$words = ["hello", "world", "php"];

foreach ($words as $word) {
    $unique = count_chars($word, 3);
    echo "$word => \"$unique\" (" . strlen($unique) . " unique)\n";
}
?>

How It Works

  1. Collect distinct bytes. Duplicates (like two ls in hello) count once.
  2. Sort by byte. Output order is ascending code, not original order.
  3. Measure diversity. strlen($unique) is how many distinct bytes appear.
  4. Same idea as mode 1. count(count_chars($s, 1)) also gives that number.

Example 3 — Anagram Check

Two strings are anagrams when their mode-1 frequency maps match.

PHP
<?php
function isAnagram(string $a, string $b): bool
{
    return count_chars($a, 1) === count_chars($b, 1);
}

$pairs = [
    ["act", "cat"],
    ["listen", "silent"],
    ["hello", "world"],
];

foreach ($pairs as [$w1, $w2]) {
    $result = isAnagram($w1, $w2) ? "yes" : "no";
    echo "$w1 / $w2 => anagram: $result\n";
}
?>

How It Works

  1. Build two maps. Mode 1 for each word.
  2. Compare with ===. Same keys and counts means same multiset of bytes.
  3. Order ignored. listen and silent rearrange the same letters.
  4. 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().

Next: crc32()

Learn how to compute a CRC32 checksum of a string.

crc32() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful