Potential gotcha with the count() function
Note, if a variable is set to FALSE and you think it's supposed to be an array and you run count() on that variable, it will return 1.
I've ran into this by running,
<?php
$array = FALSE;
if(count($array) > 0)
{
print("hello");
}
?>
$array = FALSE;
if(count($array) > 0)
{
print("hello");
}
?>
That statement is indeed true. The safest thing to do is use the is_array() function, possibly with the count() function as so.
<?php
$array = FALSE;
if(is_array($array) && count($array) > 0)
{
}
else
{
print("hello");
}
?>
$array = FALSE;
if(is_array($array) && count($array) > 0)
{
}
else
{
print("hello");
}
?>
I ran into this when executing SQLite queries and the query method returned false. I was checking to see if a table existed.
[Click to add or edit comments])
Please prepend comments below including a date