2
$a = 1;
$a OR $a = 'somthing'
echo $a; //1

Why? If = have much precedence then 'OR' then why OR execute first?

user2864740
  • 60,010
  • 15
  • 145
  • 220
  • Do you know formal grammars? If you knows formal grammar then read [my answer here](http://stackoverflow.com/questions/17162919/unambiguous-grammar-for-exponentiation-operation/17170820#17170820) I explained how precedence useful to write grammar rules. And language writer decides precedence on the basis which statement should parse how decides precedence. – Grijesh Chauhan Feb 06 '14 at 07:59
  • I added few more words in my answer please check. – Grijesh Chauhan Feb 06 '14 at 17:28

3 Answers3

2

When you put OR between two statement, if the first one returns true, the second one never will be executed.

in this case, the first statement ( $a ) returns true ( because $a = 1 ), so the

second one ( $a = 'somthing'; ) wont be executed.

Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57
  • 1
    and also keep in mind that http://docs.php.net/language.operators.precedence says: `Operator precedence and associativity only determine how expressions are grouped, they do not specify an order of evaluation.` – VolkerK Feb 06 '14 at 07:47
  • 1
    then, why 5+1*3 = 8 ? I think, * - have much precedence then +. Its right. In my case i think: $a = 'somthing' //do first (up precedence), then OR. – user3271784 Feb 06 '14 at 07:58
  • I disagree with your answer that don't explain why precedence of OR is higher then `=` – Grijesh Chauhan Feb 06 '14 at 07:59
  • `$a OR $a = 'somthing'`, here , means `if(!$a) { $a = 'somthing' }` – Alireza Fallah Feb 06 '14 at 08:08
  • 1
    @AlirezaFallah the information you added is correct and you understand correct, But just what I means it don't answers that "why precedence of OR is higher then =". this is because programmers usually like to write expressions like `$a OR $a = 'somthing'` And it should be pareses as `$a OR ($a = 'somthing')` and to impose this constant language writers decided to keep precedence of `=` higher then OR. – Grijesh Chauhan Feb 06 '14 at 08:12
1

Because 1 is truthy.

What you're saying with $a OR $a = 'somthing'; is

a is true OR set it to "somthing"

. Well, a is true, so it won't be set, while the following code would do.

$a = false;
$a OR $a = 'somthing';
echo $a; //"something"
gherkins
  • 14,603
  • 6
  • 44
  • 70
1

Because if OR has higher precedence then

$a OR $a = 'somthing'

will be parsed as:

($a OR $a) = 'somthing'

that would be technically wrong because you can't assign to an expression (while programmers would like to write expression like this is coding so it should be a valid expression).

because precedence of or operator was low hence the expression $a OR $a = 'somthing' pareses as $a OR ($a = 'somthing') And according to short-circuit first operand that is $a was evaluated as true and second operand expression not evaluated and a remains 1.

Remember precedence rules derives grammar rules and hence tells how expression will be parse. But precedence is a compile-time property that tells us how expressions are structured. Evaluation is a run-time behavior that tells us how expressions are computed (hence how expressions will be evaluates can't completely determine by precedence). And PHP docs seems to say same:

Operator Precedence
Operator precedence and associativity only determine how expressions are grouped, they do not specify an order of evaluation. PHP does not (in the general case) specify in which order an expression is evaluated and code that assumes a specific order of evaluation should be avoided, because the behavior can change between versions of PHP or depending on the surrounding code.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208