if ('constant' == $variable) vs. if ($variable == 'constant')
        Posted  
        
            by 
                Tom Auger
            
        on Programmers
        
        See other posts from Programmers
        
            or by Tom Auger
        
        
        
        Published on 2011-05-05T22:43:30Z
        Indexed on 
            2012/09/12
            21:50 UTC
        
        
        Read the original article
        Hit count: 331
        
coding-standards
Lately, I've been working a lot in PHP and specifically within the WordPress framework. I'm noticing a lot of code in the form of:
if ( 1 == $options['postlink'] )
Where I would have expected to see:
if ( $options['postlink'] == 1 )
Is this a convention found in certain languages / frameworks? Is there any reason the former approach is preferable to the latter (from a processing perspective, or a parsing perspective or even a human perspective?)
Or is it merely a matter of taste? I have always thought it better when performing a test, that the variable item being tested against some constant is on the left. It seems to map better to the way we would ask the question in natural language: "if the cake is chocolate" rather than "if chocolate is the cake".
© Programmers or respective owner