-1

I am trying to learn about php string functions, but I'm stuck.

I have this Hello World function:

<?php
$str = "Hello World!";
echo count_chars($str , 0);
?>

But the code gives following output:

Notice: Array to string conversion in /opt/lampp/htdocs/Projects/phpstringfunction/count_chars.php on line 41
Array

I cannot figure out what to do, and it is working fine in the w3schools tutorial.

mech
  • 2,775
  • 5
  • 30
  • 38
Shubham Pratik
  • 482
  • 2
  • 6
  • 18
  • 2
    count_chars with mode 0, returns an array with the ASCII value as key and number of occurrences as value. You need to read the complete documentation. They described about different modes. – Nandha Kumar Jan 19 '18 at 04:53
  • Possible duplicate of [How to echo an array in PHP?](https://stackoverflow.com/questions/9816889/how-to-echo-an-array-in-php) – mickmackusa Jan 19 '18 at 05:54

4 Answers4

0

if you want to check the return of this code use var_dump() :

try this:

$str = "Hello World!";
var_dump(count_chars($str , 0));
Dee
  • 166
  • 5
0

You can't use echo to print array in php. You count_char() with mode 0 returns an array with the ASCII value as key and number of occurrences as value.

So you need to use var_dump with count_char() for mode 0, 1 and 2, because these modes returns arrays.

For mode 3 and 4 you can simply use echo as these mode returns string.

More details here and here as well.

Sushant Pimple
  • 1,337
  • 1
  • 13
  • 26
0

count_chars — Return information about characters used in a string

SYNTAX

count_chars ( string $string [, int $mode = 0 ] )

Depending on mode count_chars() returns one of the following:

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.

Please refer http://php.net/manual/en/function.count-chars.php to know details

Swadesh Ranjan Dash
  • 544
  • 1
  • 4
  • 17
0

0 - an array with the byte-value as key and the frequency of every byte as value.

More Information

--- you can not see an array value in php by using only echo ... var_dump, print_r or loop with echo needed to see an array value .

$str = "Hello World!";
print_r(count_chars($str , 0));
Minar Mnr
  • 1,376
  • 1
  • 16
  • 23