Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.)

share|improve this question
    
if (( ($a != empty_or_null_or_notDefined && $a == 5 ) && (2 + 2 == 4) // ;-) –  Mawg Oct 28 '12 at 0:32
add comment

3 Answers 3

up vote 6 down vote accepted

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.

share|improve this answer
    
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 '12 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. –  Lattyware Oct 27 '12 at 10:56
    
Thanks. I appear not to be able to upvote yet.. –  user127379 Oct 27 '12 at 10:59
    
@user127379: but as the one who posed the question, you can "accept" an answer by clicking the checkmark under the vote marks. –  Marjan Venema Oct 27 '12 at 13:15
add comment

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.

share|improve this answer
    
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. –  Gabriel Sosa Oct 27 '12 at 12:18
    
@GabrielSosa Not for an object property. It will throw a fatal error if the object doesn't exist. –  Florian Margaine Oct 27 '12 at 15:15
    
the user is referring to a standard varible. but anyway. –  Gabriel Sosa Oct 28 '12 at 13:17
add comment

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

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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