9

I am used to Java and therefore always think conditions are interpreted from left to right, i.e. there is a vital difference in null != $obj and $obj != null

Now this seems not to be the case with PHP.

Can I do something wrong in PHP when I always start with null on the left-hand side? Can I keep my behaviour from Java or do I need to train myself to do something else when dealing with PHP conditions?

9
  • 5
    There's no difference, but you should avoid yoda conditions, it's weird you think they are standard in Java or that there's any point in them, interpretation wise.
    – yannis
    Aug 21, 2012 at 12:38
  • But woulndt that be an argument for always starting with null? Cause null = $a makes PHP choke where as $a = null would pass undetected. Aug 21, 2012 at 13:08
  • null = $a is assignment and null != $a is comparison, did you mean null == $a? null = $a won't work, and yes in assignments order of interpretation is significant - and it wouldn't work in Java either. Are we talking conditionals or assignment?
    – yannis
    Aug 21, 2012 at 13:10
  • lol ... aehm... ok. Sorry I wasn't making myself clear enough..... Lets assume you make a mistake writing a condition and create a yoda expression. (like you said) And you produce a code line like: ($a = null). It will go through unnoticed cause you produced a legal condition. Now if I tell my team to ALWAYS start a condition with null (if a null check is used) on the LEFT HAND SIDE it could help to avoid this kind of mistake. Actually... I have just answered my own question thanks to your feedback. :-) Aug 21, 2012 at 13:15
  • 1
    In what way does it differ from Java?
    – phant0m
    Aug 21, 2012 at 13:21

3 Answers 3

4
  • There is no (technical) difference – nor is there in Java to my knowledge.
  • In PHP, do not use either of these.

I recommend you always use the strict comparison operators === and !== if possible. You will know when you actually need loose comparison operators.

Misunderstandings

I believe you may be confused about a couple of things:

  • short-circuiting operators
  • order of evaluation
  • operator precedence

I just found that the PHP manual says: "PHP checks each condition in order from left to right"

No,

  • the PHP manual does not say that, it's a comment.
  • PHP does not do that.

PHP evaluates the conditions from left to right, but it stops once the result is known:

expr1 && expr2

If expr1 evaluates to false, expr2 will not be evaluated. This is called short-circuiting and it does not seem to be very well-documented – I couldn't find any other official note on the matter except for the comment in Example #1. It is a well-known feature, though.

Fallacies

I am used to Java and therefore always think conditions are interpreted from left to right, i.e. there is a vital difference in null != $obj and $obj != null


PHP checks each condition in order from left to right
Ergo: The same "best practice" can be applied

Ergo: nothing – non sequitur. The conclusion does not follow from the premise. Using constant == $variable or $variable == constant is not connected to either of short-circuiting, order of evaluation or operator precedence.

Undefined behavior

Both operands of != are always evaluated. In the case of PHP, the evaluation order (which is irrelevant here) is actually unspecified (as in absent from the documentation).

2
  • Thx for your clarification on the topic. Although I don't like you tone regarding my Java Knowledge. (See Section 15.7 of the Java Language Specification) Lets sum it up. The order of evaluation is unspecified in PHP and therefor you can not rely on it! 2nd never use != or == user === or !==. Aug 22, 2012 at 7:23
  • @anann I'm not sure where you think I'm "rating" your Java knowledge. The only thing I did say is: "nor is there in Java to my knowledge." I have read section 15.7, but it is not important here. As long as you have no side-effects in your comparison (as in your example), the order of evaluation doesn't matter. Which part do you find to be offending?
    – phant0m
    Aug 22, 2012 at 7:52
0

Regarding your particular case another approach would be to used the built-in php function is_null. So you would end up with is_null($mixedValue) which would do all the work for you and properly do type comparison and everything else.

-1

@phant0m: OK point taken... NEVER USE != or == always use !=== and === when you code with PHP. Therefore this question was wrong in the beginning, and there is no best practice with NULL on either side of the condition when it comes to PHP.

8
  • 5
    It's a tradeoff. You sacrifice readability for safety ($a == NULL encodes intent, NULL == $a adds a safeguard), and this safety isn't even that strong because you're still relying on manual work (habits) rather than uncheatable automated checks. I don't think the tradeoff is worth it, but that's a matter of personal opinion.
    – tdammers
    Aug 21, 2012 at 16:15
  • 1
    Oh, and I always assumed 'yoda condition' referred to reversing the subject and the object in your conditions, so that they read "if twenty-three apples you have" (23 == num_apples) rather than "if you have twenty-three apples" (num_apples == 23).
    – tdammers
    Aug 21, 2012 at 16:17
  • 2
    You seemed to be confused on what a "Yoda-statement" is. It is "Yoda" when you put the NULL (or the constant part of the expression) on the left: gordienoye.ca/2012/02/07/when-yoda-writes-if-statements Aug 21, 2012 at 16:41
  • 2
    Also php does warn you if you do this: stackoverflow.com/q/718415/14065 you should be checking your error logs for warnigns and errors and fixing them all. Yoda style was all the rage in the 90's. The claim was that it solved this group of problems. In reality it makes the code harder to read and the compilers (interpreters) for most languages already warn you about the problem. So there is no real benefit to using them (assuming you make sure your code runs warning free (which you should do anyway)). A better solution is not to do assignment in conditionals (thus run warning free). Aug 21, 2012 at 16:45
  • @tdammers Readability is opinion, so for those of us that have no problem with them, "yoda-conditionals" are what we tend to default to. My eyes are drawn to the conditional first, so order doesn't help or harm. Even for > and <, I'll read it more like "If that one is bigger", then read the one that should be bigger for the conditional to pass, then the other side.
    – Izkata
    Aug 21, 2012 at 19:14

Your Answer

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

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