0
0
PHPprogramming~5 mins

Operator precedence and evaluation in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AAddition (+)
BMultiplication (*)
CBoth at the same time
DDepends on variable types
What is the result of the expression: 10 - 4 - 3 in PHP?
A3
B9
C1
DCannot be determined
Which associativity does the assignment operator (=) have in PHP?
ALeft-to-right
BNo associativity
CRight-to-left
DDepends on context
In the expression: $a = 3 + 4 * 2, what is the value assigned to $a?
A14
B10
C7
D11
If two operators have the same precedence and left-to-right associativity, how are they evaluated?
AFrom left to right
BSimultaneously
CRandom order
DFrom right to left
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.