Challenge - 5 Problems
Ternary Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP code using ternary operator?
Consider the following PHP code snippet. What will it output?
PHP
<?php $score = 75; $result = $score >= 60 ? 'Pass' : 'Fail'; echo $result; ?>
Attempts:
2 left
💡 Hint
Check the condition before the question mark and what happens if true or false.
✗ Incorrect
The condition $score >= 60 is true because 75 is greater than 60, so the expression returns 'Pass'.
❓ Predict Output
intermediate2:00remaining
What does this nested ternary operator output?
Analyze the output of this PHP code with nested ternary operators.
PHP
<?php $age = 20; $status = $age < 13 ? 'Child' : ($age < 20 ? 'Teen' : 'Adult'); echo $status; ?>
Attempts:
2 left
💡 Hint
Check each condition from left to right carefully.
✗ Incorrect
Since 20 is not less than 13 and not less than 20, the last option 'Adult' is chosen.
🔧 Debug
advanced2:00remaining
What error does this PHP ternary code produce?
Identify the error caused by this PHP code snippet.
PHP
<?php $value = 10; $result = $value > 5 ? 'High' 'Low'; echo $result; ?>
Attempts:
2 left
💡 Hint
Check the syntax around the ternary operator carefully.
✗ Incorrect
The ternary operator requires a colon ':' between the true and false expressions. Missing colon causes syntax error.
❓ Predict Output
advanced2:00remaining
What is the output of this PHP code with ternary and assignment?
What will this PHP code print?
PHP
<?php $a = 0; $b = $a ?: 5; echo $b; ?>
Attempts:
2 left
💡 Hint
The ?: operator returns the left value if it is truthy, otherwise the right value.
✗ Incorrect
Since 0 is considered false in PHP, $b gets assigned 5.
🧠 Conceptual
expert2:00remaining
How many items are in the resulting array after this PHP code?
What is the count of elements in the array produced by this code?
PHP
<?php $array = [ 'a' => 1, 'b' => 2, 'c' => 3 ]; $result = []; foreach ($array as $key => $value) { $result[$key] = $value > 1 ? $value * 2 : $value; } echo count($result); ?>
Attempts:
2 left
💡 Hint
The foreach loop adds one element per original array item.
✗ Incorrect
The loop runs 3 times, adding 3 elements to $result regardless of ternary outcome.