0

I have seen multiple examples of such comparison, some other example ( from wordpress core):

if ( '' != $qv['subpost'] )
            $qv['attachment'] = $qv['subpost'];

Is code above same as:

if ( $qv['subpost'] != '' )
            $qv['attachment'] = $qv['subpost'];

or they are different in functionality?

4 Answers4

5

Some people prefer the constant == variable option, as it'll cause fatal errors if you accidentally type a = and try to do assignment:

e.g.

$a = 'foo';  // assigns 'foo' to $a
$a == 'foo'; // tests for equality
'foo' == $a // tests for equality
'foo' = $a // syntax error - assigning value to a string constant

But functionally, otherwise, there's no difference between both versions. a == b is fully equivalent to b == a.

Marc B
  • 356,200
  • 43
  • 426
  • 500
0

Yes, they do the same thing. It checks to see if $qv['subpost'] contains a value in both examples. No difference at all unless you're Yoda.

SeanWM
  • 16,789
  • 7
  • 51
  • 83
0

There is no difference.

(A == B) == (B == A)

The only thing that someone can put the value first is the readability, for example:

if ( 'APPLE' == $var ) {

} else if ('BANANA' == $var) {

}
hsz
  • 148,279
  • 62
  • 259
  • 315
0

There is no functional difference. You are comparing equality, and they will either be equal or not, no matter what side of the operator the values are on.

This question comes down to code style. Personally, when comparing against static values I prefer to always have the variable on the left. Others disagree. Use whatever style is in place in the project you're working on.

Brad
  • 159,648
  • 54
  • 349
  • 530