Reputation: 348
Why can't I use empty ($example->body
), and need to use $example->body->isEmpty()
to check for empty array. What is the return type that causes it can't use empty() and need to rely on isEmpty()
?
I do know this is from Laravel Collection based on this post. But didn't really understand why behind the scene. Any enlighten is greatly appreciated (but I'll try to understand it from freshgrad POV)
Upvotes: 1
Views: 184
Reputation: 8850
Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.
In the manual they provided things that are considered to be empty. object
type is not part of it. Instance of Laravel collection (Illuminate\Support\Collection
) is considered as a object
type. It doesn't matter if the object is plain empty object for PHP
, the empty()
will always returns false
Upvotes: 1
Reputation: 13259
empty()
cannot work because it always returns true
Because if you dd($example); you'll notice you'll notice an instance of Illuminate\Support\Collection is always returned, even when there are no results. Essentially what you're checking is $a = new stdClass; if ($a) { ... } which will always return true.
Upvotes: 1