dsij89625 2019-07-19 09:57
浏览 59
已采纳

关于COUNT('')的事情

I found a question in php7.3.4.

if ( count( '' ) == 1 )
{
    echo 1;
}  else {
    echo 2;
}

This program output 1.Then I checked the source for the function 'count'.

PHP_FUNCTION(count)
{
    ....
    switch (Z_TYPE_P(array)) {
        case IS_NULL:
            php_error_docref(NULL, E_WARNING, "Parameter must be an array or an object that implements Countable");
            RETURN_LONG(0);
        break;
        case IS_ARRAY:
            ...
        break;
        case IS_OBJECT:
            ....
        break;
        default:
            php_error_docref(NULL, E_WARNING, "Parameter must be an array or an object 
            that implements Countable");
            RETURN_LONG(1);
        break;
}

This is easy to producing BUG. I think it count('') maybe output error or 0 is better than 1 and a Warning. What do you think about? And count(NULL) is not equal to count(''),It feels strange.

  • 写回答

1条回答 默认 最新

  • duan5991518 2019-07-19 10:01
    关注

    From count() docs:

    Returns the number of elements in array_or_countable. When the parameter is neither an array nor an object with implemented Countable interface, 1 will be returned.

    count('') thus returns 1, just like count('seventeen') returns 1. Working as intended.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度