Check if array is empty

Check if array is empty

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

Stefan Bogdanescu

Founder & Senior Architect

Why if ($rows == "") Fails and How to Check Arrays Correctly

A common stumbling block when working with database results is attempting to verify if a data collection is empty using string comparison logic. The code snippet provided demonstrates a classic mistake: checking an array against an empty string.

if($rows == ""){
    echo "No Data";
}

This condition will almost never behave as intended in PHP. Arrays are distinct data structures from strings; they do not hold a value of "" simply because they contain no elements. Comparing an array to an empty string using the loose equality operator (==) might trigger type juggling, but it is unreliable and semantically incorrect. The if statement fails because $rows is still an array object in memory, regardless of its length.

To correctly determine if a collection holds data, you must check its count or existence. The most robust approach for PHP 5.4 and later is using the empty() construct. This function evaluates whether a variable is set to false, null, 0, "0", "", [] (an empty array), or if it is not set at all.

if (empty($rows)) {
    echo "No Data";
}

This handles the specific case of an initialized but empty array perfectly. If you prefer a more explicit check regarding the number of elements, count() combined with a strict comparison to zero is equally valid and often preferred in type-safe environments:

if (count($rows) === 0) {
    echo "No Data";
}

Handling Legacy Database Drivers

The source code utilizes the deprecated mysql_* functions. These extensions were removed from PHP core in version 7.0 due to security vulnerabilities and lack of support for prepared statements. If you are maintaining legacy systems, ensure you wrap these checks immediately after fetching data or switch to mysqli or PDO.

With mysqli, the logic remains identical regarding the empty check, but the driver change is critical for production stability:

$result1 = $db->query("SELECT * FROM TestPhase WHERE Pid < 10");
while($row = mysqli_fetch_array($result1)) {
    $rows[] = [
        'id' => $row['id'],
        'parent' => $row['parent'],
        'name' => $row['name'],
    ];
}

if (empty($rows)) {
    echo "No Data";
}

Checking Arrays in JavaScript

When this data is transferred to the client-side via JSON, it arrives as a JavaScript array stored in a variable like treeData. The logic for checking emptiness shifts from PHP's empty() or loose comparison to specific JavaScript type checks.

The pattern if (treeData) is empty shown in the source material is syntactically invalid in JavaScript. You cannot use natural language keywords inside an if statement. Furthermore, a standard truthy check (if (treeData)) will fail if the array exists but contains no items, because an empty array [] evaluates to true in a boolean context.

To hide the button only when the array is truly empty, you must check the length property or use the built-in Array.isArray() method combined with a length check.

if (treeData.length === 0) {
    $("button").hide();
}

Alternatively, if there is a possibility that treeData might be undefined rather than an empty array, a nullish coalescing approach or optional chaining ensures safety:

if (!Array.isArray(treeData) || treeData.length === 0) {
    $("button").hide();
}

Trade-offs and Best Practices

Using empty() in PHP is convenient but can be slightly less performant than count() === 0 for very large datasets if you are concerned about the overhead of checking multiple falsy values. However, for typical database result sets, the difference is negligible. The primary benefit of empty() is readability; it clearly communicates the intent to check for absence without needing to calculate a count manually.

In JavaScript, relying on .length is the standard convention. Checking for null or undefined separately is only necessary if the variable itself might not be initialized in the scope where you are checking it. If treeData is always guaranteed to be an array (perhaps via strict typing or framework constraints), a simple length check is sufficient and performs well.

If you are building a Laravel application, these patterns integrate seamlessly with Eloquent collections as well. An empty collection behaves like an empty array, so the same empty() logic applies when iterating over query results processed by the ORM.

Our network