Which "if" is better

Discussion in 'PHP' started by xrvel, May 19, 2008.

  1. #1
    Recently, i opened some WordPress files.

    It uses "if" like this
    
    if ('john' == $name) {
    } else if ('doe' == $name) {
    }
    
    PHP:
    My question is which is better, the WP style, or the normal style :
    
    if ($name == 'john') {
    } else if ($name == 'doe') {
    }
    
    PHP:
    There must be a reason so that most WP files use their own "if" style :D

    Any opinion is appreciated
     
    xrvel, May 19, 2008 IP
  2. Perow

    Perow Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It really doesn't matter. Both will work as well. However, my preference goes to the second way, where the comparison is more logical.
     
    Perow, May 19, 2008 IP
  3. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #3
    Thanks for the quick response.

    I am curious too, whether the wp style is faster in execution time or something like that (i have not tested the execution time btw)
     
    xrvel, May 19, 2008 IP
  4. lanmonkey

    lanmonkey Active Member

    Messages:
    549
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #4
    The best way is whats best for you, as it will be you who will have to potentially bugfix and troubleshoot the code
     
    lanmonkey, May 19, 2008 IP
  5. ksamir2004

    ksamir2004 Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hello,

    Main thing is which is more Effectively. As per my knowledge 2nd option is more suitable then 1st.

     
    ksamir2004, May 19, 2008 IP
  6. lanmonkey

    lanmonkey Active Member

    Messages:
    549
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #6
    you mean which is faster?

    in real world terms I dont think it matters as you will probably shave 0.0001 ms off script execution time.

    its not even worth worrying about. I can see no idea why WP does it differnetly, probably just a historical thing.
     
    lanmonkey, May 19, 2008 IP
  7. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The reason they probably do it is that there is a school of thought that says when making comparisons against a constant, use the constant first.

    This is to help pick up when they miss an equal sign and make it an assignment, not a comparison. In this case, with the constant first, you will get an error.

    eg.
    Code is meant to be:
    if ( $name == 'John ) {

    but is accidentally typed as:
    if ( $name = 'John' ) {

    this is valid code and accepted by the parser, even though it doesn't do what it was expected to do. On the other hand, if they wanted to type:
    if ( 'John' == $name ) {

    but typed;
    if ( 'John' = $name ) {
    it would fail, as you can't assign a different value to 'John'.

    It's just one of many ways to help pick up subtle coding mistakes.
     
    TwistMyArm, May 19, 2008 IP
    xrvel likes this.
  8. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #8
    It makes absolutely no difference at all ... variables are usually first in conditions, but either way is correct ...
     
    krakjoe, May 20, 2008 IP
  9. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #9
    That sounds logical. Thanks for the explanation. :)

    Thank you for all of your opinion. I appreciate it. :)
     
    xrvel, May 20, 2008 IP