开发者

Why does count() return more than 0 when there is no element in an array?

开发者 https://www.devze.com 2023-04-11 18:02 出处:网络
If there are no elements in a ar开发者_运维知识库ray, why does count() return more than 0? if (count($_POST[\'tickboxes\']) > 0) {

If there are no elements in a ar开发者_运维知识库ray, 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.


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";
}


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.


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号