Open In App

PHP count_chars() Function

The count_chars() is an inbuilt function in PHP and is used to perform several operations related to string like the number of an ASCII character occurs in a string. Syntax :

count_chars(string,return_mode);

Parameters: The count_chars() function takes two parameters string and return_mode as explained below:



Return Type: This function will return an array or string depending on the parameter return_mode as described above. Examples:

Input : string = "GeeksforGeeks"  ,  return_mode = 3
Output : Gefkors

Below is the PHP program to illustrate the working of count_chars() function: 






<?php 
    // PHP program to illustrate count_chars() 
      
    // Input string 
    $string = "geeksforgeeks"
  
    // return_mode 1 
    print_r(count_chars($string,1)); 
  
    // return_mode 3 
    print_r(count_chars($string,3)); 
  
    // return_mode 4 
    print_r(count_chars($string,4)); 
?> 

Output:

Array
(
    [101] => 4
    [102] => 1
    [103] => 2
    [107] => 2
    [111] => 1
    [114] => 1
    [115] => 2
)

efgkors

!"#$%&'()*+,-./0123456789:;?@ABCDEFGHIJKLMNOPQRSTUVWXY
Z[\]^_`abcdhijlmnpqtuvwxyz{|}~??????????????????????
????? ¡¢£¤¥¦§¨©ª«¬­®¯´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×
ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ

Time Complexity: O(n) where n is the size of the string.

The above program shows the return values for string “geeksforgeeks” with return_mode as 1, 3 and 4. You can modify the program by changing the value of return_mode in the function call to see the returned values for modes 0 and 2 also.

Article Tags :