Just a bit of fun, wondered which of these people like best for checking if an array is empty in #php? There's (debatably :D) no right answer...

#1 is short
#2 and #3 are very readable

Leaning towards #2 myself!

Hopefully I typed the example code properly lol. xD

@scottwhat I prefer #1 : I like terse code.
@scottwhat I generally use 1, because I'm lazy. Arguably 3 is most correct(tm). Don't use 2.
@Crell
@scottwhat
I would write it like this:
´
if (count($array)) $emptyArray = false;
´
Seems my way of thinking is somehow different, I don't know 😁
@amarok @scottwhat That's a reasonable compromise.

@Crell @amarok @scottwhat first one also use implicit cast, by using `!`
Even when checking if an array is not empty with just `if($array)`, it an implicit cast. I always use a boolean value in condition.

So I will compare the array to an empty array or if number of elements is 0.

I think that will be future proof if any implicit cast / behavior change in PHP engine 🙂

@velkuns @Crell @amarok Sounds good! A strict and future-proof comparison.
@Crell @scottwhat why don't you recommend 2?
@luceos @scottwhat empty() has a lot of implicit behavior in it that uses extra weak typing. Lots of coding guides even say to avoid it.
@Crell @luceos I think you've convinced me #3 is best here haha.
@Crell @scottwhat all those tutorials are based on an era before 8.0, implying hardly any type hinting. Code written these days ought to have clear type hinting and using empty on an array in that case makes perfectly sense from a readability side. Then take into consideration performance as well https://stackoverflow.com/a/2216159
Checking for empty arrays: count vs empty

This question on 'How to tell if a PHP array is empty' had me thinking of this question Is there a reason that count should be used instead of empty when determining if an array is empty or not? My

Stack Overflow
@luceos @Crell Interesting stuff, thanks! It's great that PHP offers more strict options now.
@luceos @scottwhat Perf benchmarks have changed even more than typing. And the engine folks routinely make improvements so such micro optimizations are backwards from what they were previously. I wouldn't trust benchmarks from the 5.x era for anything. The engine was half rewritten since then.

@Crell @scottwhat recent benchmarks show the same results though, aside from the loose type checking, empty is fastest with the logical NOT having similar results. The strict comparison is slightly faster than count, but not faster than empty.

I personally hold on to using empty, as type hinting should be a standard nowadays in agreement to: https://phpc.social/@Crell/114869999327725709

Recent php 8.3 performance comparison: https://benjamincrozat.com/php-array-empty

Larry Garfield (@[email protected])

It's 2025. If your library still doesn't have types in it (parameter, return, and property), I assume it's abandoned and I should not use it. There are no exceptions to this statement. Not typing your PHP code in 2025 is irresponsible. No, docblocks are not good enough. #PHP

PHP Community on Mastodon
@scottwhat You could also have `if ($array === [])` :)
@Girgias I think this seems like a really great option as it specifically asks if the variable is an empty array. :)