The ternary operator helps you choose between two values quickly in one line. It makes your code shorter and easier to read.
Ternary operator in PHP
Introduction
Syntax
PHP
$result = (condition) ? value_if_true : value_if_false;
The condition is checked first.
If the condition is true, the value after the question mark (?) is used.
If the condition is false, the value after the colon (:) is used.
Examples
$status to 'Adult' if $age is 18 or more, otherwise 'Minor'.PHP
$age = 20; $status = ($age >= 18) ? 'Adult' : 'Minor'; echo $status;
PHP
$score = 75; $message = ($score >= 60) ? 'Pass' : 'Fail'; echo $message;
PHP
$is_logged_in = false; echo $is_logged_in ? 'Welcome back!' : 'Please log in.';
Sample Program
This program checks if the temperature is above 25. If yes, it says 'Hot', otherwise 'Cold'.
PHP
<?php $temperature = 30; $weather = ($temperature > 25) ? 'Hot' : 'Cold'; echo "The weather is $weather."; ?>
Important Notes
You can nest ternary operators but it can make code hard to read.
Use parentheses to make complex conditions clear.
Summary
The ternary operator is a short way to choose between two values.
It uses the format: condition ? value_if_true : value_if_false.
It helps keep code simple and clean for quick decisions.
Practice
1. What does the ternary operator
condition ? value_if_true : value_if_false do in PHP?easy
Solution
Step 1: Understand the ternary operator format
The ternary operator uses a condition followed by a question mark, then two values separated by a colon.Step 2: Identify its purpose
It returns the first value if the condition is true, otherwise the second value.Final Answer:
It chooses one of two values based on the condition's truth. -> Option AQuick Check:
condition ? true_value : false_value = chooses value [OK]
Hint: Think: condition true? pick first : else pick second [OK]
Common Mistakes:
- Confusing ternary with loops
- Thinking it defines functions
- Assuming it comments code
2. Which of the following is the correct syntax for a ternary operator in PHP?
easy
Solution
Step 1: Recall ternary operator syntax
The correct syntax is: condition ? value_if_true : value_if_false;Step 2: Check each option
Only condition ? value_if_true : value_if_false; matches the correct syntax exactly.Final Answer:
condition ? value_if_true : value_if_false; -> Option CQuick Check:
Syntax = condition ? true : false [OK]
Hint: Remember: question mark before colon in ternary [OK]
Common Mistakes:
- Swapping question mark and colon
- Using comma instead of colon
- Adding extra parentheses incorrectly
3. What is the output of this PHP code?
$age = 20;
$result = ($age >= 18) ? 'Adult' : 'Minor';
echo $result;
medium
Solution
Step 1: Evaluate the condition
$age is 20, so ($age >= 18) is true.Step 2: Apply ternary operator
Since condition is true, $result is set to 'Adult'.Final Answer:
Adult -> Option AQuick Check:
20 >= 18 ? 'Adult' : 'Minor' = 'Adult' [OK]
Hint: Check condition result true or false to pick output [OK]
Common Mistakes:
- Assuming output is the number 20
- Confusing true and false branches
- Expecting syntax error
4. Find the error in this PHP code using ternary operator:
$score = 75;
$grade = $score > 60 ? 'Pass' 'Fail';
echo $grade;
medium
Solution
Step 1: Check ternary operator syntax
The ternary operator requires a colon ':' between the true and false values.Step 2: Identify the missing colon
Code has 'Pass' 'Fail' without ':' causing syntax error.Final Answer:
Missing colon ':' between true and false values -> Option BQuick Check:
Syntax needs ? true : false [OK]
Hint: Always put ':' between true and false parts [OK]
Common Mistakes:
- Omitting colon ':'
- Using comma instead of colon
- Confusing variable names
5. Given an array of numbers, which PHP code correctly uses the ternary operator to create a new array labeling each number as 'Even' or 'Odd'?
hard
Solution
Step 1: Understand the condition for even or odd
A number is even if $n % 2 == 0, otherwise odd.Step 2: Check each option's condition and labels
$numbers = [1,2,3]; $labels = array_map(fn($n) => $n % 2 == 0 ? 'Even' : 'Odd', $numbers); correctly labels even numbers as 'Even' and odd as 'Odd'. Others swap or invert labels.Final Answer:
$numbers = [1,2,3]; $labels = array_map(fn($n) => $n % 2 == 0 ? 'Even' : 'Odd', $numbers); -> Option DQuick Check:
Even check with ternary = correct labeling [OK]
Hint: Even numbers have remainder 0 when divided by 2 [OK]
Common Mistakes:
- Swapping 'Even' and 'Odd' labels
- Using wrong modulo condition
- Confusing truthy/falsy values in ternary
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
