Why does json_decode return null for empty array?

后端 未结 5 1232
萌比男神i
萌比男神i 2021-01-12 00:27

Why would this echo \"NULL\"? In my would it would be decoded to an empty array.

Is it something obvious I\'m missing?



        
5条回答
  •  遥遥无期
    2021-01-12 01:13

    This is because array()==NULL. It does not check the object type in this case.

    gettype(null) returns null, whereas

    gettype(array()) returns array. Hope you got the difference.

    Probably what you need is

    if ($json_decoded === null) {
       echo "NULL";
    } else
    {
       echo "NOT NULL";
    }
    

提交回复
热议问题