← BACK
Laravel / Utilities

Use blank() and filled() Instead of empty() in Laravel

Punyapal Shah • • 1 min read
edit this tip →

PHP's empty() treats 0, '0', and false as empty. Laravel's blank() and filled() helpers handle these values intuitively without silent bugs.

PHP's native empty() is notoriously loose: it considers 0, '0', and false as empty, causing subtle bugs when validating quantities, scores, or boolean toggles.

Laravel provides blank() and filled() as intuitive, string-trimmed alternatives.


Comparison Matrix

// PHP empty() pitfalls:
empty(0);       // true  (oops: 0 is a valid quantity!)
empty('0');     // true  (oops: '0' is a valid string!)
empty('   ');   // false (whitespace is NOT considered empty by PHP)

// Laravel blank():
blank(0);       // false (0 is a real value)
blank('0');     // false ('0' is a real value)
blank('   ');   // true  (automatically trims whitespace)
blank('');      // true
blank(null);    // true
blank([]);      // true

// Laravel filled() (exact inverse of blank()):
filled(0);      // true
filled('hello');// true
filled('   ');  // false

Usage in Controllers

// Clean validation without false positives on 0
if (blank($request->input('score'))) {
    return response()->json(['error' => 'Score is required'], 422);
}

Related Tips

View all tips →
Laravel Utilities

Use Immutable Dates by Default in Laravel

Configure Laravel's Date facade to use CarbonImmutable by default, and automate migrating legacy Carbon calls with Rector.

READ >

// Got a tip in mind? Contributions are always welcome