5

First off, sorry if this is answered somewhere else. I did a brief search, but wasn't sure how to ask in search terms.

I'm looking at some code and came across lot's of statements like this:

if ( ($a != empty_or_null_or_notDefined && $a == 5 )

Is this the same as just saying:

if ( $a == 5 )

?

(language is PHP.)

1
  • if (( ($a != empty_or_null_or_notDefined && $a == 5 ) && (2 + 2 == 4) // ;-)
    – Mawg
    Oct 28, 2012 at 0:32

3 Answers 3

6

At a purely logical level, presuming & is the local and operator, then $a being 5 precludes it from being null.

That said, in some languages, and short circuits (that is, if one operand fails, it does not check the others, as the whole clause is known to fail when one does). In this case, this could be a move for efficiency, although it's highly unlikely equality is an expensive enough operation to make this worthwhile (especially as PHP doesn't offer operator overloading).

As it's PHP, a single ampersand is a bitwise and operation, not a logical one, as such, it will not be lazy. Your topic has one ampersand, while the question itself has two, I think the latter is more likely to be the real option (as what is happening is a logical check), but you should clarify.

3
  • Interesting. Actually would it cause performance issues/hits?Because the expressions are quite simple, two terms, and the empty check feels redundant...
    – user127379
    Oct 27, 2012 at 10:55
  • 2
    @user127379 Almost certainly. It's extra code serving no purpose, it just makes it less clear and probably slightly slower. Don't get me wrong - it's going to be insignificant performance wise, but it is entirely pointless. In a language where == could be overloaded to a very expensive operation, this might be worth doing, but I'd argue that the design would need to be changed.
    – Latty
    Oct 27, 2012 at 10:56
  • @user127379: but as the one who posed the question, you can "accept" an answer by clicking the checkmark under the vote marks. Oct 27, 2012 at 13:15
4

This code is necessary to avoid errors. If you try to access an undefined object, you get a fatal error.

Checking whether this object is not undefined before checking the value allows you to work around this error.

If the first check doesn't pass (is it undefined?), it won't check the value, thus will not throw an error.

3
  • this is not true. Unless you are codding with the STRICT mode on and caring about this messages, PHP will just notice you about an undefined var and move forward. In any event in PHP you check if the variable is defined because is a good practice not because it won't run. Oct 27, 2012 at 12:18
  • @GabrielSosa Not for an object property. It will throw a fatal error if the object doesn't exist. Oct 27, 2012 at 15:15
  • the user is referring to a standard varible. but anyway. Oct 28, 2012 at 13:17
2

Nice question.

Logic A # if ( ($a != empty_or_null_or_notDefined && $a == 5 )

Logic B # if ( $a == 5 )

Logic B is better than Logic A. (Regarding Algorithm complexity) If your purpose is only Logic B, then you don't need Logic A.

NULL is critical. It can be stated as Undefined, Empty, etc. Better to escape NULL comparison, because it is hard to debug.

I support @Florian Margaine's answer, absolutely, Every code is necessary to avoid errors. If you try to access an undefined object, you get a fatal error. Checking whether this object is not undefined before checking the value allows you to work around this error.

And if comparison to NULL is necessary to you, then please have a look at the following resource

https://stackoverflow.com/questions/381265/better-way-to-check-variable-for-null-or-empty-string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.