Recall & Review
beginner
What is operator precedence in PHP?
Operator precedence determines the order in which different operators in an expression are evaluated. Operators with higher precedence are evaluated before those with lower precedence.
Click to reveal answer
beginner
Which operator has higher precedence: multiplication (*) or addition (+)?
Multiplication (*) has higher precedence than addition (+), so it is evaluated first in an expression.
Click to reveal answer
beginner
How does PHP evaluate the expression: 3 + 4 * 2?
PHP first multiplies 4 * 2 = 8, then adds 3 + 8 = 11. So the result is 11.
Click to reveal answer
intermediate
What happens when operators have the same precedence?
When operators have the same precedence, PHP evaluates them based on their associativity, which can be left-to-right or right-to-left.
Click to reveal answer
intermediate
Explain the difference between left-to-right and right-to-left associativity with an example.
Left-to-right associativity means operators are evaluated from left to right. For example, in 10 - 5 - 2, PHP evaluates (10 - 5) - 2 = 3. Right-to-left associativity means evaluation goes from right to left, like the assignment operator: $a = $b = 5 assigns 5 to $b first, then $a.
Click to reveal answer
In PHP, which operator is evaluated first in the expression: 5 + 3 * 2?
✗ Incorrect
Multiplication (*) has higher precedence than addition (+), so 3 * 2 is evaluated first.
What is the result of the expression: 10 - 4 - 3 in PHP?
✗ Incorrect
Subtraction is left-associative, so PHP evaluates (10 - 4) - 3 = 6 - 3 = 3.
Which associativity does the assignment operator (=) have in PHP?
✗ Incorrect
The assignment operator (=) is right-to-left associative, so assignments happen from right to left.
In the expression: $a = 3 + 4 * 2, what is the value assigned to $a?
✗ Incorrect
Multiplication happens first: 4 * 2 = 8, then addition: 3 + 8 = 11, so $a = 11.
If two operators have the same precedence and left-to-right associativity, how are they evaluated?
✗ Incorrect
Operators with left-to-right associativity are evaluated from left to right.
Describe how PHP decides the order of operations in an expression with multiple operators.
Think about which operators PHP looks at first and how it handles ties.
You got /4 concepts.
Explain the difference between operator precedence and associativity with simple examples.
Precedence is about which operator goes first; associativity is about direction when operators tie.
You got /4 concepts.