0
0
PHPprogramming~20 mins

Operator precedence and evaluation in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP code with mixed operators?

Consider the following PHP code snippet. What will it output?

PHP
<?php
$result = 3 + 4 * 2 / (1 - 5) ** 2 ** 3;
echo $result;
?>
A7
B11
C3.0001220703125
DSyntax Error
Attempts:
2 left
💡 Hint

Remember that exponentiation (**) is right-associative and has higher precedence than multiplication and division.

Predict Output
intermediate
2:00remaining
What is the output of this PHP code with logical and comparison operators?

What will this PHP code print?

PHP
<?php
$a = true;
$b = false;
$c = false;
$result = $a && $b || $c;
echo (int)$result;
?>
A1
B0
CSyntax Error
Dnull
Attempts:
2 left
💡 Hint

Logical AND (&&) has higher precedence than logical OR (||).

🔧 Debug
advanced
2:00remaining
What error does this PHP code raise due to operator precedence?

Examine the following PHP code. What error will it produce when run?

PHP
<?php
$value = 5;
if ($value > 3 && $value < 10 || $value = 0) {
    echo 'Valid';
} else {
    echo 'Invalid';
}
?>
AWarning: Assignment inside condition without parentheses
BNo error, outputs 'Valid'
CParse error: syntax error
DOutputs 'Invalid'
Attempts:
2 left
💡 Hint

Assignment (=) inside conditions can cause warnings if not grouped properly.

Predict Output
advanced
2:00remaining
What is the output of this PHP code with ternary and concatenation?

What will this PHP code print?

PHP
<?php
$score = 85;
$message = 'Result: ' . $score > 80 ? 'Pass' : 'Fail';
echo $message;
?>
APass
BResult: Pass
CFail
DResult: 1
Attempts:
2 left
💡 Hint

Concatenation (.) has lower precedence than comparison (>) and ternary (?:).

Predict Output
expert
2:00remaining
What is the value of $x after this PHP code runs?

Analyze this PHP code and determine the final value of $x.

PHP
<?php
$x = 2;
$x *= $x + 3;
echo $x;
?>
A10
B25
C7
DSyntax Error
Attempts:
2 left
💡 Hint

Remember that the right side of the *= operator is evaluated before multiplication.