PHP Operator Precedence and Associativity Practice Questions with Solutions

Introductions

Operator precedence and associativity determine how PHP evaluates expressions containing multiple operators. For example, in 10 + 5 * 2, PHP performs multiplication before addition, so the result is 20, not 30. When operators have the same precedence, associativity helps determine the evaluation order. In this chapter, you will practice 10 PHP questions to understand these rules and avoid common calculation mistakes. PHP Operator Precedence and Associativity Practice Questions with Solutions to help you understand the concepts.

Question 1: Understand Operator Precedence with Addition and Multiplication

Problem Statement: Write a PHP program to calculate the result of 10 + 5 * 2 and explain why multiplication is performed first.

PHP Solution:

<?php
$result = 10 + 5 * 2;

echo "Result: " . $result;
?>

Sample Output:

Result: 20

Explanation: Multiplication (*) has higher precedence than addition (+). PHP first calculates 5 * 2, which equals 10, and then adds 10.

Calculation: 10 + (5 * 2) = 20.

Concepts Covered: Operator precedence, multiplication, and addition.

Question 2: Change the Evaluation Order Using Parentheses

Problem Statement: Write a PHP program to calculate (10 + 5) * 2. Explain how parentheses change the result.

PHP Solution:

<?php
$result = (10 + 5) * 2;

echo "Result: " . $result;
?>

Sample Output:

Result: 30

Explanation: Parentheses have higher precedence than multiplication. PHP evaluates the expression inside the parentheses first.

Calculation: (10 + 5) * 2 = 15 * 2 = 30.

Compare this with Question 1, where the result was 20.

Concepts Covered: Parentheses, precedence, and expression evaluation.

Question 3: Understand Division and Addition Precedence

Problem Statement: Write a PHP program to evaluate 20 + 12 / 4. Display the result.

PHP Solution:

<?php
$result = 20 + 12 / 4;

echo "Result: " . $result;
?>

Sample Output:

Result: 23

Explanation: Division (/) has higher precedence than addition (+).

PHP calculates 12 / 4 = 3 first and then adds 20.

Calculation: 20 + (12 / 4) = 23.

Concepts Covered: Division, addition, and operator precedence.

Question 4: Evaluate an Expression with Multiple Operators

Problem Statement: Write a PHP program to evaluate 8 + 4 * 3 - 6 / 2.

PHP Solution:

<?php
$result = 8 + 4 * 3 - 6 / 2;

echo "Result: " . $result;
?>

Sample Output:

Result: 17

Explanation: Multiplication and division have higher precedence than addition and subtraction.

PHP evaluates the expression as follows:

  • 4 * 3 = 12
  • 6 / 2 = 3
  • 8 + 12 - 3 = 17

Concepts Covered: Multiple operators, arithmetic precedence, and expression evaluation.

Question 5: Understand Associativity with Subtraction

Problem Statement: Write a PHP program to evaluate 20 - 5 - 3. Explain why the result is 12.

PHP Solution:

<?php
$result = 20 - 5 - 3;

echo "Result: " . $result;
?>

Sample Output:

Result: 12

Explanation: Subtraction has left-to-right associativity. When operators have the same precedence, PHP evaluates this expression from left to right.

Calculation: (20 - 5) - 3 = 15 - 3 = 12.

It does not evaluate 20 - (5 - 3), which would produce 18.

Concepts Covered: Associativity, subtraction, and evaluation order.

Question 6: Understand Associativity with Division

Problem Statement: Write a PHP program to evaluate 100 / 10 / 2 and display the result.

PHP Solution:

<?php
$result = 100 / 10 / 2;

echo "Result: " . $result;
?>

Sample Output:

Result: 5

Explanation: Division has left-to-right associativity.

PHP evaluates (100 / 10) / 2, giving 10 / 2 = 5.

Using parentheses differently, such as 100 / (10 / 2), would give 20.

Concepts Covered: Division, left associativity, and parentheses.

Question 7: Understand Comparison and Logical Operator Precedence

Problem Statement: Write a PHP program to evaluate the expression $age >= 18 && $age <= 60 for an age of 25.

PHP Solution:

<?php
$age = 25;

$result = $age >= 18 && $age <= 60;

var_dump($result);
?>

Sample Output:

bool(true)

Explanation: Comparison operators such as >= and <= have higher precedence than logical AND (&&).

PHP first checks whether the age is at least 18 and whether it is at most 60. Both conditions are true, so the final result is true.

Concepts Covered: Comparison operators, logical AND, precedence, and Boolean values.

Question 8: Understand the Difference Between && and and

Problem Statement: Write a PHP program to demonstrate how the precedence of && and and can affect the result of an assignment.

PHP Solution:

<?php
$a = true;
$b = false;

$result1 = $a && $b;
$result2 = $a and $b;

var_dump($result1);
var_dump($result2);
?>

Sample Output:

bool(false)
bool(true)

Explanation: In PHP, && has higher precedence than assignment (=), while and has lower precedence than assignment.

  • $result1 = $a && $b; assigns the result of $a && $b, which is false.
  • $result2 = $a and $b; assigns $a to $result2 first. The and expression is evaluated afterward, so $result2 remains true.

This is a useful example of why operator precedence matters. When writing conditions, prefer && when you intend the logical operation to be evaluated before assignment.

Concepts Covered: Logical operators, assignment precedence, and associativity.

Question 9: Understand the Precedence of the Concatenation Operator

Problem Statement: Write a PHP program to combine a label with a calculated total. Use parentheses to make the intended calculation clear.

PHP Solution:

<?php
$price = 100;
$quantity = 3;

$message = "Total: Rs. " . ($price * $quantity);

echo $message;
?>

Sample Output:

Total: Rs. 300

Explanation: The parentheses ensure that PHP calculates $price * $quantity before concatenating the result with the text.

Calculation: 100 * 3 = 300, so the final message is Total: Rs. 300.

Using parentheses around calculations is a good practice because it makes the intended evaluation order clear and easier to read.

Concepts Covered: String concatenation, multiplication, parentheses, and expression clarity.

Question 10: Evaluate a Combined Arithmetic Expression

Problem Statement: A shop sells 4 products at ₹50 each and gives a discount of ₹30. Write a PHP program to calculate the final bill using operator precedence.

PHP Solution:

<?php
$quantity = 4;
$price = 50;
$discount = 30;

$total = $quantity * $price - $discount;

echo "Final Bill: Rs. " . $total;
?>

Sample Output:

Final Bill: Rs. 170

Explanation: Multiplication has higher precedence than subtraction, so PHP calculates the product total first.

  • Product total: 4 * 50 = 200
  • Discount: 30
  • Final bill: 200 - 30 = 170

Parentheses can make the calculation even clearer: ($quantity * $price) - $discount.

Concepts Covered: Operator precedence, multiplication, subtraction, and practical billing calculations.

Key Takeaways

  • Operator precedence determines which operators are evaluated first in an expression.
  • Associativity determines the grouping or evaluation direction when operators have the same precedence.
  • Multiplication and division are generally evaluated before addition and subtraction.
  • Parentheses can explicitly control the order of calculations.
  • Subtraction and division are left-associative, so expressions are grouped from left to right.
  • Comparison operators are evaluated before logical AND (&&).
  • The logical operators && and and have different precedence levels in PHP.
  • The concatenation operator (.) has its own precedence rules, so parentheses can improve clarity.
  • Writing clear expressions with parentheses helps prevent unexpected results.

Frequently Asked Questions (FAQs)

1. What is operator precedence in PHP?

Operator precedence determines which operator PHP evaluates first when an expression contains multiple operators.

2. What is associativity in PHP?

Associativity determines how operators with the same precedence are grouped, such as evaluating subtraction from left to right.

3. Which has higher precedence: multiplication or addition?

Multiplication has higher precedence than addition, so PHP performs multiplication first unless parentheses change the order.

4. How can I change operator precedence in PHP?

Use parentheses to force a particular part of an expression to be evaluated first.

5. What is the difference between precedence and associativity?

Precedence determines which operator takes priority. Associativity determines how operators of equal precedence are grouped.

6. What is the result of 20 - 5 - 3 in PHP?

The result is 12 because subtraction is left-associative: (20 - 5) - 3.

7. Why should I use parentheses in PHP expressions?

Parentheses make calculations easier to understand and help ensure that expressions are evaluated in the intended order.

8. Do logical operators have different precedence levels in PHP?

Yes. For example, && has higher precedence than and, which can affect how assignments and conditions are evaluated.

9. Does operator precedence affect string concatenation?

Yes. Concatenation has a precedence level of its own. Using parentheses around calculations before joining them with text makes the intended behavior clear.

10. Why is operator precedence important for beginners?

It helps beginners predict expression results correctly, avoid calculation mistakes, and write more reliable PHP programs.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top