#43538 [Opn]: count on non-array should return false, not 1
| From: | kalle@php.net | Date: | Wed, 16 Jul 2008 00:32:22 +0000 |
| Subject: | #43538 [Opn]: count on non-array should return false, not 1 | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-126800@lists.php.net to get a copy of this message | ||
ID: 43538
Updated by: kalle@php.net
Reported By: warren at transfusionmedia dot com
Status: Open
Bug Type: Feature/Change Request
Operating System: Win XP SP2
PHP Version: 5.2.5
New Comment:
Seems odd why its coded to return 1 imo, heres a patch against latest
PHP_5_3 cvs that changes this to return false:
http://www.phpfi.com/332583
Previous Comments:
------------------------------------------------------------------------
[2007-12-08 20:30:47] warren at transfusionmedia dot com
Description:
------------
When doing count($var) where $var is not an array, the return = 1.
The problem is when you're doing a check in your code:
if (count($var) > 0){
//do something
}
This will evaluate as true if $var is not an array. This is a problem
if you are doing this:
if (count($var) > 0){
sort($var); // anything that requires $var to be an array
}
If there is anything in that if statement that requires $var to be an
array, there will be a fatal error. I know you can also add an is_array
check in there, but that is non-obvious and shouldn't be necessary.
Really, if $var is not an array, count($var) should return false or
null, or at least a -1
Reproduce code:
---------------
$var = "foo";
if (count($var) > 0){
sort($var);
} else {
echo "The value of var is not greater than zero";
}
Expected result:
----------------
The value of var is not greater than zero
Actual result:
--------------
Warning: sort() expects parameter 1 to be array, string given
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=43538&edit=1