Skip to main content
11 events
when toggle format what by license comment
Nov 24, 2020 at 14:33 comment added Zollie @mickmackusa - if we use it the way you just gave, you are absolutely right. I’m at least convinced :)
Nov 24, 2020 at 9:30 comment added mickmackusa !empty($a) && isset($a) versus !empty($a) will ALWAYS have the same result. Give me an $a value that makes the conditions give a different boolean result. I'll save you some time, it's not possible. !empty() && isset() is an anti-pattern that should not exist in any code (similar to switch(true) blocks). I am not here to war; I just want to prevent researchers learning an anti-pattern. I would like to delete all of these comments once I have convinced you.
Nov 24, 2020 at 8:41 comment added Lodder @mickmackusa - It depends on the data being returned. I don't know if extra_fields is only an object property if it has data, or whether it's set to null, or whether it's set to 0, so I've added the additional isset() as a sanity check. Have a careful read of php.net/manual/en/types.comparisons.php .....more specifically the $x = 0; and $x = 1; and note the difference. It's simply a matter of providing an answer with the information I have provided to me. As mentioned before, feel free to post another answer, but this discussion will not force me to edit mine.
Nov 24, 2020 at 8:03 comment added mickmackusa There is a misunderstanding in what I am trying to explain. If you want 0, don't use empty(). If you are going to use !empty() then there is absolutely 100% no reason to also call isset(). I am not telling any fibs here. If you aren't calling !empty(), then isset() can certainly be useful. If you don't believe me, read the previously posted Stack Overflow link -- it spells it all out. Truth: !empty() is enough for this task. There is no convincing me otherwise, because this is a provable fact. I cannot accurately answer until the value of the variable is posted in the question.
Nov 24, 2020 at 7:06 comment added Zollie @Lodder - please do not take these comments personal, your answer is the most optimal approach on this question. But the question itself is so invaluable (sorry to say that) that it does not worth to write more answers on this, since these warning should not be seen on production sites at all in the first place as error reporting should be turned off to not show these PHP warnings... and the K2 developers should refine these codes as you did in your answer...
Nov 24, 2020 at 6:54 comment added Zollie @mickmackusa - I thought that you will notice that (you did it few times earlier at other posts and I agree with you mostly on that) however as @Lodder mentioned there are few cases where we should use both checks together, like when you have an empty string for example: ”” the variable is set. So the isset() is satisfied (because the value is not null) but the !empty() condition is not satisfied. So there can be few exceptions from the “never”...
Nov 24, 2020 at 6:11 comment added Lodder You're both welcome to write additional answers
Nov 24, 2020 at 6:08 comment added Lodder It is never necessary to write both checks. That's completely incorrect. They are two different methods, performing two different tasks. So yes, you can use them in the same condition if you need to. !empty() will reject 0, which is a completely valid value.
Nov 23, 2020 at 20:11 comment added mickmackusa It is never necessary to write both checks (isset() and !empty()) in a condition: !empty($this->item->extra_fields) && isset($this->item->extra_fields). stackoverflow.com/a/4559976/2943403 If !empty($this->item->extra_fields) is satisfied, then isset($this->item->extra_fields) will always be satisfied. If a variable is declared/set (and there is evidence that it is because there is no warning of accessing an undeclared property), then checking an array for non-emptiness is as simple as if ($this->item->extra_fields) {
Nov 23, 2020 at 18:29 comment added Zollie Probably in this case it is better to pre-check whether the $var is countable too with using is_countable($var) method.
Nov 23, 2020 at 17:52 history answered Lodder CC BY-SA 4.0