From: warren at transfusionmedia dot com
Operating system: Win XP SP2
PHP version: 5.2.5
PHP Bug Type: Feature/Change Request
Bug description: count on non-array should return false, not 1
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 bug report at http://bugs.php.net/?id=43538&edit=1
--
Try a CVS snapshot (PHP 4.4):
http://bugs.php.net/fix.php?id=43538&r=trysnapshot44
Try a CVS snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=43538&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=43538&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=43538&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=43538&r=fixedcvs
Fixed in release:
http://bugs.php.net/fix.php?id=43538&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=43538&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=43538&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=43538&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=43538&r=support
Expected behavior: http://bugs.php.net/fix.php?id=43538&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=43538&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=43538&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=43538&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=43538&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=43538&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=43538&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=43538&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=43538&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=43538&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=43538&r=mysqlcfg