When to use laravel filled() Vs PHP empty()

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

When to use filled() Vs PHP empty(): The Developer's Guide to Robust Validation

As developers, we constantly grapple with how to correctly validate data in PHP. Two functions that frequently come up in this discussion are empty() and concepts related to checking if a field is truly "filled," often represented by custom checks or framework methods like Laravel’s validation rules. While both deal with the concept of emptiness, they serve fundamentally different purposes. Choosing the right tool ensures your application logic is robust, secure, and handles edge cases gracefully.

This post will delve into the nuances between using empty() versus checking for a "filled" state, explaining the rationale behind each choice from a senior developer's perspective.


Understanding PHP’s empty() Function

The empty() function is one of the most frequently used tools in PHP for checking variable states. However, it is crucial to understand what it checks for:

$name = '';
$email = null;
$age = 0;
$data = [];

var_dump(empty($name)); // bool(false)
var_dump(empty($email)); // bool(true) - Checks for null, false, "", 0, and empty arrays.
var_dump(empty($age));   // bool(false) - Note: 0 is considered "empty" by empty(), but we often need to distinguish between a missing value and a valid zero.
var_dump(empty($data));  // bool(true)

The Limitation of empty(): The primary issue with empty() is that it treats many different states as invalid. For example, if you are validating an age field, empty(0) returns true, meaning a valid age of 0 would be incorrectly flagged as empty. Similarly, in database interactions or API responses, checking for mere existence (is the key present?) is different from checking for meaningful content (does the key hold actual data?).

The Concept of "Filled" Data

When we talk about a field being "filled," we are usually referring to more stringent criteria than just being non-empty. A field is truly filled if it exists, has been provided by the user/system, and contains meaningful, relevant data that satisfies business logic constraints.

In modern frameworks like Laravel, this concept is handled through explicit validation rules rather than relying solely on basic PHP functions. For instance, when setting up a FormRequest, you define exactly what constitutes a valid input, which moves the responsibility from generic emptiness checks to specific domain constraints.

When to Choose Which: A Practical Comparison

Use empty() for Simple Existence Checks

Use empty() when you only need a quick, general check to see if a variable has any value assigned, regardless of its content. This is suitable for simple initialization or basic form checks where zero or empty strings are acceptable placeholders.

Example: Checking if a session variable was set:

if (empty($_SESSION['user_id'])) {
    // User has not logged in; redirect to login
    return redirect('/login');
}
// This works fine for simple presence checks.

Use Explicit Checks or Framework Validation for Business Logic

Use explicit checks, type casting, or framework validation (like Laravel’s required, numeric, or custom rules) when you need to enforce specific business rules. If a field must contain a positive integer greater than zero, relying on empty() is insufficient.

Example: Validating an input must be a positive number:

$input = $_POST['quantity'] ?? 0;

// Check if the value exists AND is numerically greater than zero
if (!isset($_POST['quantity']) || !is_numeric($_POST['quantity']) || $_POST['quantity'] <= 0) {
    // Handle error: Quantity must be a positive number
    throw new \Exception("Invalid quantity provided.");
}

// If we were using Laravel, this would be handled by validation rules applied to the Request object.

Conclusion: Choosing Precision Over Generality

The choice between empty() and checking for "filled" data boils down to precision. Use empty() for quick sanity checks on variable existence, but reserve complex, business-critical validation for explicit checks or powerful framework mechanisms. As you build sophisticated applications—especially those leveraging the structure provided by frameworks like Laravel—moving beyond simple Boolean checks to defining precise data constraints is essential for writing maintainable and secure code. Always aim to validate what the data means, not just if it exists.