There are several ways to test that an array is empty in PHP, and the one I see most often is:
$var = [];
if (empty($var)) { ... }
Enter...
For further actions, you may consider blocking this person and/or reporting abuse
why not:
You can.
It will force PHP to cast your variable in Boolean, which I want to avoid (for pretty the same reason to not use
empty
).And I find it more explicit.
I find it kind of explicit that an empty array "means" false
The problem is not that empty function returns false for an empty array, the problem is that empty function does to too many thing (not just on array).
Thanks for your comment !
The problem is "There are some instances where empty works well.". In some case, it works well, but sometimes it cause a bug and you search after the root cause.
A lot of PHP beginners don't know how it works exactly and will have bugs because of the versatility of this function.
Many senior developers that I know prohibit the
empty
function, for the reasons that I explained.It's just advice, then I'll let you do what you want ;)
A lot of PHP beginners don't know how works "if" or "for", so lets not use it...
I have an other article for these beginners, who don't know how to make a loop : dev.to/klnjmm/never-write-another-... 🤣
I agree. I do not want to do 3 different validations since it is there and always has a scenario to fit
empty()
in my codebase (don't ask me why).I'd like to quote a famous singer who says important words regarding this statement:
What is just "3 different validations" in order to avoid bugs ?
just cast to bool
much more safe than empty & checks "", 0, null, false... etc. cases
This demonstrates a really good and safe way to use
empty
. In this case, we have a nullable string property. But, it could even be unset. In this case,getBar
should return the default value if$f->bar
is unset, null, or an empty string. This does exactly what it's supposed to do, and it's safe, because everything is typed. The only potential error here is the case of a typo, and that's what tools like phpstan (or your own IDE) are for.In case it wasn't obvious, the alternative would be:
You can't tell me that's better code.
It is a lot of combination that make the use of
empty
safe.For you second example,
isset
method test that the value is notnull
.So it’s just
Not so dirty 😉
I had forgotten that
isset
stupidly returns false for null values.Yes, not quite so bad, but I still prefer
empty
in this case.Ah yes, the annoying case where an explicit
null
value is different from an unset value. But if you have an object or array where you need to see if a property exists, this can be quite useful.For objects/classes where you don't need the distinction,
property_exists
is the function you want.This is one of those weird edge cases in dynamic programming languages. For the most part, I'll take the unset vs explicit null in PHP over the undefined/null split in Javascript.
Some months ago, I sent a proposal to php.dev for updating the empty function, because of internal flaws and semantic inconsistence. The new version I named is_empty_obj fixes all above.
Then roll your own!
When we talk about clean & readable code, we try to avoid large comparisons. So empty() function help us in that journey. I know it´s not perfect because it can´t eval objects in the same way, so if you don´t want to be thinking when use it or not, instead eval two or three differents options, you can use your own Empty function, extending it in some core class or redefining with runkit7_function_redefine (I prefer first option).
Something like:
If you want to try:
empty function is FOR ME not « clean code ». It’s not a matter of comparing object, it’s because it tests too much thing and that can cause bugs.
Like I describe in the article, if you find your comparaison too « big », you can extract it in a method with a understandable name.
If you have to compare object, I think you can do it otherwise.
And for you, what is an « empty » object ? A object with all attributes equal to null ?
I love the
empty
function!😆🤣
I spent waaay too long trying to mirror the discussion.
I really hope this turns up in a technical interview someplace, somewhen.
I disagree. You should understand what you're doing and use the proper functions for each case. The
empty
isn't bad.As an argument: someone will copy your "right method"
if (count($var) === 0) {...}
as a "silver bullet" and apply it to an array with thousands of elements (because Jimmy said that's the solution). It will cause huge performance issues.Thanks for your reply.
Maybe I’m wrong, but count on a large array is not slower than count on a small array. The size of an array is stored internally, PHP does not count the number of items each time you call the function.
I implemented this function to check "emptiness" for different variable types.
It is part of my libs since the last Fall. It works like a charm.
I replaced any call to standard empty() function.