Bird
Raised Fist0
PHPprogramming~10 mins

Ternary operator in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Ternary operator
Evaluate condition
Yes↓
Use value if true
↓
Result
No↓
Use value if false
↓
Result
The ternary operator checks a condition and returns one value if true, another if false.
Execution Sample
PHP
<?php
$age = 20;
$status = ($age >= 18) ? 'Adult' : 'Minor';
echo $status;
?>
This code checks if age is 18 or more, then sets status to 'Adult' or 'Minor' accordingly.
Execution Table
StepConditionCondition ResultValue if TrueValue if FalseResult assigned to $status
1$age >= 1820 >= 18 is True'Adult''Minor''Adult'
2End of expression---Execution stops
💡 Condition is true, so 'Adult' is assigned to $status and execution ends.
Variable Tracker
VariableStartAfter Step 1Final
$age202020
$statusundefined'Adult''Adult'
Key Moments - 2 Insights
Why does $status get 'Adult' and not 'Minor'?
Because the condition $age >= 18 is true at step 1 in the execution_table, so the true value 'Adult' is chosen.
What if $age was 16? What would happen?
If $age was 16, the condition would be false, so the false value 'Minor' would be assigned to $status instead.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $status after step 1?
A20
B'Minor'
C'Adult'
Dundefined
💡 Hint
Check the 'Result assigned to $status' column in row 1 of execution_table.
At which step does the ternary operator decide the value of $status?
AStep 2
BStep 1
CBefore Step 1
DAfter Step 2
💡 Hint
Look at the 'Condition Result' and 'Result assigned to $status' columns in execution_table.
If $age was 15, what would be the result assigned to $status?
A'Minor'
B15
C'Adult'
Dundefined
💡 Hint
Think about the condition $age >= 18 and what happens if it is false.
Concept Snapshot
Ternary operator syntax: (condition) ? value_if_true : value_if_false;
It evaluates the condition once.
Returns value_if_true if condition is true.
Returns value_if_false if condition is false.
Useful for simple if-else in one line.
Full Transcript
The ternary operator in PHP is a shortcut for if-else. It checks a condition and returns one value if true, another if false. In the example, we check if $age is 18 or more. Since $age is 20, the condition is true, so $status is set to 'Adult'. If $age was less than 18, $status would be 'Minor'. This operator helps write simple decisions in one line.

Practice

(1/5)
1. What does the ternary operator condition ? value_if_true : value_if_false do in PHP?
easy
A. It chooses one of two values based on the condition's truth.
B. It creates a loop that runs twice.
C. It defines a function with two parameters.
D. It comments out a line of code.

Solution

  1. 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.
  2. Step 2: Identify its purpose

    It returns the first value if the condition is true, otherwise the second value.
  3. Final Answer:

    It chooses one of two values based on the condition's truth. -> Option A
  4. Quick 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
A. condition ? value_if_true, value_if_false;
B. condition : value_if_true ? value_if_false;
C. condition ? value_if_true : value_if_false;
D. condition ? (value_if_true : value_if_false);

Solution

  1. Step 1: Recall ternary operator syntax

    The correct syntax is: condition ? value_if_true : value_if_false;
  2. Step 2: Check each option

    Only condition ? value_if_true : value_if_false; matches the correct syntax exactly.
  3. Final Answer:

    condition ? value_if_true : value_if_false; -> Option C
  4. Quick 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
A. Adult
B. Error
C. 20
D. Minor

Solution

  1. Step 1: Evaluate the condition

    $age is 20, so ($age >= 18) is true.
  2. Step 2: Apply ternary operator

    Since condition is true, $result is set to 'Adult'.
  3. Final Answer:

    Adult -> Option A
  4. Quick 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
A. Wrong variable name used
B. Missing colon ':' between true and false values
C. Ternary operator cannot be used with strings
D. Missing semicolon after echo

Solution

  1. Step 1: Check ternary operator syntax

    The ternary operator requires a colon ':' between the true and false values.
  2. Step 2: Identify the missing colon

    Code has 'Pass' 'Fail' without ':' causing syntax error.
  3. Final Answer:

    Missing colon ':' between true and false values -> Option B
  4. Quick 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
A. $numbers = [1,2,3]; $labels = array_map(fn($n) => $n % 2 == 1 ? 'Even' : 'Odd', $numbers);
B. $numbers = [1,2,3]; $labels = array_map(fn($n) => $n % 2 == 0 ? 'Odd' : 'Even', $numbers);
C. $numbers = [1,2,3]; $labels = array_map(fn($n) => $n % 2 ? 'Even' : 'Odd', $numbers);
D. $numbers = [1,2,3]; $labels = array_map(fn($n) => $n % 2 == 0 ? 'Even' : 'Odd', $numbers);

Solution

  1. Step 1: Understand the condition for even or odd

    A number is even if $n % 2 == 0, otherwise odd.
  2. 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.
  3. Final Answer:

    $numbers = [1,2,3]; $labels = array_map(fn($n) => $n % 2 == 0 ? 'Even' : 'Odd', $numbers); -> Option D
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard