14

A string is an array of characters, correct? If so, then why does count() not produce the same result as strlen() on a string?

6
  • 19
    PHP doesn't always make sense like you want it to ;-) Commented Jun 24, 2011 at 11:40
  • @Matt Ellen it's the same for any lang, why not learn why there are different functions for different tasks? Commented Jun 24, 2011 at 11:42
  • @Raoul: what's the same? That different types have different functions applicable to them? Sure, but in some languages lists/arrays of characters and lists/arrays of anything else can be treated the same. Commented Jun 24, 2011 at 11:47
  • 1
    @Matt Ellen for clarification, no language makes sense if people jump to conclusions regarding syntax/features. Commented Jun 24, 2011 at 12:28
  • @Matt Ellen exactly, I was agreeing with you. Commented Jun 24, 2011 at 13:20

7 Answers 7

27

Unless it is an object, count casts its argument to an array, and

  count((array) "example")
= count(array("example"))
= 1
Sign up to request clarification or add additional context in comments.

1 Comment

This answer is much better than the accepted "not an array" answer, and is the correct answer.
15

A string is an array of characters in C, C++ and Java. In PHP, it is not. Remember that PHP is a very loose language, you can probobly get a character from a PHP string with the []-selector, but it still dosn't make it an array.

2 Comments

I hate it when languages do that. When I see array notation, I automatically think I'm working with an actual array. It doesn't help that the PHP manual does not explicitly say "Strings ARE NOT arrays, despite having an indexer."
There are very few languages where a string is the same as an array. Maybe in C, but any higher level programming language will have a special structure for strings. Likewise, in C++, std::string can be accessed like an array, yet sizeof(std::string) won't give you the lenght. Just use the right tool for the right job.
3

count() counts the number of entries in an Array.

$test = array(1,2,3);
echo count($test);

Output: 3

Why would you want to use count() on a string when strlen() can do that? Are you not sure if your input is a string or an array? Then use is_array() to check that.

5 Comments

Yes, I know. And if a string is an array of characters, then count() should return the number of characters in that string.
You could call a string an array of characters in English maybe, but in PHP it's not an array of characters, it's a string.
Kokos, the question is more about why strings aren't treated the same as normal arrays when they could be, in PHP. Haskell, for example, treats strings as a list of characters, so you can use all of Haskell's list functions on strings.
a string is not an array of characters, it can be converted to an array natively, but count() accept only as argument an array and strlen() a string
I was thrown off by the fact that strings have an indexer, and that the manual didn't explicitly say that despite the indexer, a string is not an array.
1

How exactly a string is being handled internally by a specific programming language, must not necessarily mean you can handle it equally to therefore "related" data types. What you describe may be possible in plain C. However PHP is not C and so is not following the same characteristics.

Comments

1

Strings are just a series of charactes, and count only counts number of elements in an array.

using $string[$index]; its just a shortcut kinda of thing to help you find Nth character,

you could use count(explode('',$string)); which presumably is what strlen does

so lesson for today is

count == array length

strlen == string length

Comments

0

count gets the number of elements in an array, srtlen gets the length of a string. This is in the docs:

http://php.net/manual/en/function.strlen.php

3 Comments

Yes, I know. I was asking WHY that is the case.
@kevinmajor1 again, this is covered in the docs, if you read them.
I have read the docs. They just weren't as clear on the matter as I had hoped. See my comments above for why I immediately thought that strings were, indeed, character arrays.
0

count is more preferabaly user for array value count strlen its count only the no of char in str.....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.