All operators process their operators in a certain direction. This direction is called associativity, and it depends on the type of operator. Most operators are processed from left to right, which is called left associativity. For example, in the expression 3 + 5-2, 3 and 5 are added together, and then 2 is subtracted from the result, evaluating to 8. While left associativity means that the expression is evaluated from left to right, right associativity means the opposite.

Since the assignment operator has right associativity, it is one of the exceptions because right associativity is less common. The expression $a=$b=$c processes by $b being assigned the value of $c, and then $a being assigned the value of $b. This assigns the same value to all of the variables. If the assignment operator is right associative, the variables might not have the same value.

If you're thinking that this is incredibly complicated, don't worry. These rules are enforced only if you fail to be explicit about your instructions. Keep in mind that you should always use brackets in your expressions to make your actual meaning clearer. This helps both PHP and also other people who may need to read your code.

If you accidentally use & instead of &&, or | instead of ||, you'll end up getting the wrong operator. & and | compare binary data bit by bit. PHP will convert your operands into binary data and apply binary operators.

Was this article helpful?

0 0

Post a comment