0

I have read the documentation for the empty() function, but I'm not sure if I understood it correctly.

Is this how the empty() function works?:

  • empty() will return true if the variable doesn't exist.
  • If the variable exist, then empty() will return true if the variable is equal to false or if the variable is equal to 0.
  • If the variable exist and the variable is not equal to false and the variable is not equal to 0, then the variable is converted to bool and if the result of the conversion is false, then empty() will return true, else empty() will return false.
Steven
  • 71
  • 3
  • 3
    What are the doubts that you have? Can you give an example of what isn't clear to you? – Nigel Ren Jan 22 '23 at 16:34
  • The documentation _used to list_ what was considered to be `empty` beyond a variable that does not exist, but that has gone away for some reason. Anyway, the first user-provided comment at the documentation link as has the list. – Paul T. Jan 22 '23 at 16:37
  • @PaulT. there's a list of values that are equivalent to false at [Converting to boolean](https://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting). The comment omits float. The table you reference is probably the one at [PHP type comparison tables](https://www.php.net/manual/en/types.comparisons.php) under _Comparisons of $x with PHP functions_. – Markus AO Jan 22 '23 at 17:04
  • Does this answer your question? [Why check both isset() and !empty()](https://stackoverflow.com/questions/4559925/why-check-both-isset-and-empty) – Markus AO Jan 22 '23 at 17:09
  • @MarkusAO ... yes, thanks, that is a good table comparable reference that I haven't seen in awhile. (and has likely been updated since then) – Paul T. Jan 22 '23 at 17:13
  • @PaulT. yeah it seems to have grown since I last saw it too. I'm wondering if there's a "PHP Manual Memory Lane" project somewhere, not the first "I'm sure it was there" effect. Digging in, [that list under `empty` was removed in 2020](https://github.com/php/doc-en/commit/3ae0b14dd06ea9d47728f366dfe2c29476a2468e) "to avoid duplication", a moot edit methinks, rather _duplicate more if it adds clarity to a manual entry_. Anyway. ^_^ – Markus AO Jan 22 '23 at 17:23
  • If I'm not wrong, `empty()` just follows the same conversion rules that apply everywhere else in the language, I honestly think a link is a better than a copy that will eventually get out of sync. Fun fact: it's a **language construct** rather than a function; that's why it can be fed a non-existing variable and not throw an error. – Álvaro González Jan 23 '23 at 14:56

0 Answers0