1

If there are no elements in a array, why does count() return more than 0?

if (count($_POST['tickboxes']) > 0) {
           echo "found";
}

$POST array structure:

Array
(
    [ID] => 53
    [tickboxes] => 
    [dropdowns] => 
)

I was expecting 0 or null.

2
  • Put result of var_dump($_POST['tickboxes']); in your question. I bet there is a string inside Commented Oct 6, 2011 at 12:30
  • $_POST['tickboxes'] is set, any set variable will at least return 1 with count. Unset variables will return 0 (variables which contain NULL are not set in PHP). Arrays and Objects will return their count. Please read the documentation of the return value. Commented Oct 6, 2011 at 12:51

3 Answers 3

9

You are trying to countDocs something that is not an array, but rather a string.

As documented, count returns 1 for strings, regardless of their lengths.

You can use the strlenDocs function instead, as it counts the number of ascii characters in that string:

if (strlen($_POST['tickboxes']) > 0) {
           echo "found";
}

Additionally, you can use the emptyDocs language construct for this, it will check if it's an empty array or a blank string, or the integer 0 or the string '0' - and that last one may cause you grief (depending on what you are doing with it, i.e. if your users can send you such input).

If empty would be an option for you, you can just spare that as well:

if ($_POST['tickboxes']) {
           echo "found";
}

Don't forget to check if that key in the $_POST array exists if you do so. If you're unsure, empty won't give you any warning:

if (!empty($_POST['tickboxes'])) {
           echo "found";
}
Sign up to request clarification or add additional context in comments.

5 Comments

count does not convert the first parameter, it just accepts a value and will return as documented.
@hakre it's documented that way because behind the scenes anything that is not an array is converted to an array first. Instead of confusing users with those details they just documented what the net result is.
Ariel, what you say is wrong. It's checked against the type with a switch statement and then returned accordingly. There is no conversion into an array.
@hakre You are correct, and I am surprised. I guess they did it that way as an optimization since there is no reason to convert it only to ignore the result.
As long as I remember count worked that way. An older description was something like "the number of values" and skalars just have one value. You normally use it with arrays or objects, maybe that's why this comes now on top of the manual page. Especially with objects this is nice (countable interface).
3

That would be because your variable is not an array. Try echo count(''); and you'll see it returns 1 while count(array()) will be 0.

Comments

0

i think @Ariel solve it

if you sending data from html form you can use tamperdata addons in firefox

https://addons.mozilla.org/en-US/firefox/addon/tamper-data/

this will help you :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.