PHP count_chars() Function

Last Updated:

PHP count_chars()

The count_chars() function returns information about characters used in a string.

This function counts the number of occurrences of every byte-value (0 to 255) in string and returns it in various ways (depending upon the specified mode parameter).


PHP Compiler
<?php $str = "PHP world"; echo count_chars($str, 3); ?>


count_chars(str, mode)

Parameter Values

str Required Specifies a string to be examined.
mode Optional Specifies the return modes.
The following are the list of different modes:
  • 0 - an array with the byte-value as key and the frequency of every byte as value.
  • 1 - same as 0 but only byte-values with a frequency greater than zero are listed.
  • 2 - same as 0 but only byte-values with a frequency equal to zero are listed.
  • 3 - a string containing all unique characters is returned.
  • 4 - a string containing all not used characters is returned.
Default value is 0.

Return Value

array | string The return value depends on the specified mode parameter.

Share this Page

Meet the Author