I found a question in php7.3.4
.
if ( count( '' ) == 1 )
{
echo 1;
} else {
echo 2;
}
This program output 1.Then I checked the source for the function 'count'.
PHP_FUNCTION(count)
{
....
switch (Z_TYPE_P(array)) {
case IS_NULL:
php_error_docref(NULL, E_WARNING, "Parameter must be an array or an object that implements Countable");
RETURN_LONG(0);
break;
case IS_ARRAY:
...
break;
case IS_OBJECT:
....
break;
default:
php_error_docref(NULL, E_WARNING, "Parameter must be an array or an object
that implements Countable");
RETURN_LONG(1);
break;
}
This is easy to producing BUG. I think it count('')
maybe output error or 0 is better than 1 and a Warning. What do you think about? And count(NULL)
is not equal to count('')
,It feels strange.
From count()
docs:
Returns the number of elements in
array_or_countable
. When the parameter is neither an array nor an object with implementedCountable
interface,1
will be returned.
count('')
thus returns 1
, just like count('seventeen')
returns 1
. Working as intended.