How to check that an object is empty in PHP?

How to check that an object is empty in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why empty() Fails on SimpleXML and How to Actually Check for Empty Data

When working with SimpleXMLElement objects loaded via simplexml_load_file(), relying on standard PHP constructs like empty($obj) or checking if the object is false is a common source of subtle bugs. The core issue lies in how PHP treats XML nodes that have no children versus those that contain empty elements.

In your specific scenario, where $obj holds XML data loaded from a URL, the result of simplexml_load_file() depends entirely on the structure of the remote document. If the XML file contains valid tags but no text content or child nodes—such as a root element like <root><child/></root>—simplexml_load_file() returns an object instance, not false. Consequently, the variable $obj is truthy. Running empty($obj) will return false, even though the document carries no meaningful data within its structure. Conversely, if the XML file is completely malformed or missing entirely, the function returns false, and empty() correctly identifies that state.

To accurately determine if an XML object represents "empty" content (i.e., valid syntax with zero nodes), you must inspect the node count rather than the object's existence. The most robust approach involves counting the children of the root element. You can iterate through the direct children of the $obj instance and verify that none exist.

Here is a practical implementation pattern:

$obj = simplexml_load_file($url);

if ($obj === false) {
    // Handle load failure (invalid XML or network error)
    return;
}

// Check for child nodes
$count = count((array) $obj);

if ($count > 0) {
    // The object exists and has content
    $data = $obj->childElementName(); 
} else {
    // The object exists but is effectively empty
    // You can treat this as an empty response here
}

The cast (array) is critical here. SimpleXML objects are not standard arrays, so passing them directly to count() without conversion might yield unexpected results depending on the PHP version and specific node configuration. Converting the object to an array ensures you are counting the actual child nodes rather than properties defined on the object itself.

Another frequent pitfall involves distinguishing between an empty string inside a tag and a missing tag. Consider the difference between <item> </item> and <item/>. When parsed by SimpleXML, both often result in similar boolean evaluations if not inspected closely. However, checking for child elements is usually sufficient because self-closing tags or tags containing only whitespace typically do not register as having children in the context of a root-level check.

If you are using Laravel to handle this logic, you might find that passing raw XML strings through SimpleXMLElement directly into collection methods can sometimes lead to confusion regarding empty states. In such cases, normalizing the data into a standard array or collection structure immediately after parsing often simplifies subsequent checks. This decouples your business logic from the quirks of the DOM API.

Ultimately, the decision to use node counting over boolean emptiness checks aligns with how XML documents are structured. An empty document is one that fails to contain any data nodes, not necessarily one that fails to parse. By verifying the count of child elements, you ensure your application handles both missing files and valid but content-less XML responses correctly, avoiding logic errors downstream in your data processing pipeline.

Our network