How to check that an object is empty in PHP?

前端 未结 11 2050
不思量自难忘°
不思量自难忘° 2020-11-29 03:31

How to find if an object is empty or not in PHP.

Following is the code in which $obj is holding XML data. How can I check if it\'s empty or not?

11条回答
  •  -上瘾入骨i
    2020-11-29 04:26

    If you cast anything in PHP as a (bool), it will tell you right away if the item is an object, primitive type or null. Use the following code:

    $obj = simplexml_load_file($url);
    
    if (!(bool)$obj) {
        print "This variable is null, 0 or empty";
    } else {
        print "Variable is an object or a primitive type!";
    }
    

提交回复
热议问题