Home > Software design >  When to use laravel filled() Vs PHP empty()
When to use laravel filled() Vs PHP empty()

Time:07-19

The filled function Vs using the empty function. What is the reason for choosing filled Vs !empty()?

CodePudding user response:

I think they basically do the same work. The only difference that I noticed is that the filled() method is a Laravel's helper function and only available on a $request instance or in the Illuminate\Http\Request class. While the empty() method is available globally because its a PHP's helper function. You can use empty() on any variable in any class or controller. While on the other hand filled() can only be used wherever you're receiving a request or you have to manually create an instance of Request class.

Personally, I've never used the filled() method, so I can't tell you exactly what is the technical difference between them (if there is any).

CodePudding user response:

The big difference between filled or its inverse blank and empty is what is considered empty.

For example:

filled(0);    // true
blank(0);     // false
!empty(0);    // false

filled('  '); // false;
blank('  ');  // true;
!empty('  '); // true;

filled('');   // false;
blank('');    // true;
!empty('');   // false;

The functions work in a very different way, so it's not a matter of just picking one of the two. It depends on what you are trying to do in your code.

CodePudding user response:

empty example

$test = array(
1 => '',
2 => "",
3 => null,
4 => array(),
5 => FALSE,
6 => NULL,
7=>'0',
8=>0,
);

foreach ($test as $k => $v) {
    if (empty($v)) {
        echo "<br> $k=>$v is empty";
    }
}
/**
Output
1=> is empty
2=> is empty
3=> is empty
4=>Array is empty
5=> is empty
6=> is empty
7=>0 is empty
8=>0 is empty
**/


if(isset($test)) // return true because $test is defined
if(is_null($test)) // return false because $test is not null
  • Related