3

From the docs -

int strlen ( string $string )

it takes string as a parameter, now when I am doing this-

$a = array('tex','ben');
echo strlen($a);

Output - 5

However I was expecting, two type of output-

  1. If it is an array, php might convert it into string so the array will become-

    'texben' so it may output - 6

  2. If 1st one is not it will convert it something like this -

    "array('tex','ben')" so the expected output should be - 18 (count of all items)

But every time it output- 5

My consideration from the output is 5 from array word count but I am not sure. If it is the case how PHP is doing this ?(means counting 5)

Community
  • 1
  • 1
Trialcoder
  • 5,816
  • 9
  • 44
  • 66

3 Answers3

10

The function casts the input as a string, and so arrays become Array, which is why you get a count of 5.

It's the same as doing:

$a = array('tex','ben');

echo (string)$a; // Array
var_dump((string)$a); // string(5) "Array"

This is the behavior prior to PHP 5.3. However in PHP 5.3 and above, strlen() will return NULL for arrays.

From the Manual:

strlen() returns NULL when executed on arrays, and an E_WARNING level error is emitted.

Prior [to 5.3.0] versions treated arrays as the string Array, thus returning a string length of 5 and emitting an E_NOTICE level error.

Community
  • 1
  • 1
MrCode
  • 63,975
  • 10
  • 90
  • 112
3

Use

$a = array('tex','ben');
$lengths = array_map('strlen',$a);

to get an array of individual lengths, or

$a = array('tex','ben');
$totalLength = array_sum(array_map('strlen',$a));

to get the total of all lengths

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • 3
    Why complicate things when: `strlen(implode($a))` will work fine? – Leri Jul 01 '13 at 11:18
  • Well if the OP wants an array of individual string lengths, the first solution isn't complicating things; the second is a natural extension from that, though I'll conceded it is more complicated than implode() and strlen() – Mark Baker Jul 01 '13 at 11:22
  • @MarkBaker +1 for two new methods..i will investigate more abt these two soon :) – Trialcoder Jul 01 '13 at 11:23
  • @MarkBaker It depends on what OP wants. If only requirement is to count characters in array with this structure I'd go with implode. Personally I hate passing closures as strings (i.e. passing names of pre-defined functions) it makes debugging nightmare in large projects. – Leri Jul 01 '13 at 11:27
  • 1
    @PLB - though using a closure as the first argument simply to do the strlen is even more ugly – Mark Baker Jul 01 '13 at 11:30
  • I just tried a comparison of the 2 methods on an array of 40k elements. `strlen(implode($a))` was about 12 times faster than `array_sum(array_map('strlen',$a))` so definitely better to use the `strlen(implode())` method! – Redzarf Apr 12 '18 at 14:01
2

The array is implicitly converted to a string. In PHP this yields the output Array, which has 5 letters as strlen() told you.

You can easily verify this, by running this code:

$a = array('tex','ben');
echo $a;
Sirko
  • 72,589
  • 19
  • 149
  • 183