I realise strlen
is the correct function to use, I'm just curious as to why this happens.
How come if PHP in I do echo count("");
, it returns the value 1?
I'd have expected it to return 0
, as opposed to 1
.
count is counting how many element there are. Here you got a string, so one element.
Please read the doc :
Returns the number of elements in array_or_countable. If the parameter is not an array or not an object with implemented Countable interface, 1 will be returned. There is one exception, if array_or_countable is NULL, 0 will be returned.
As the documentation says
If the parameter is not an array or not an object with implemented Countable interface, 1 will be returned.
Probably, as a consequence of this:
var_dump((array)"");
... printing this:
array(1) {
[0]=>
string(0) ""
}
See Type Juggling for the dirty details. The official answer is that 1
is simply the documented return value:
If the parameter is not an array or not an object with implemented Countable interface, 1 will be returned. There is one exception, if array_or_countable is NULL, 0 will be returned.