📖 Scenario: You are creating a simple PHP script to check if a person is old enough to vote. Voting age is 18 years or older.
🎯 Goal: Build a PHP script that uses the ternary operator to decide if a person can vote based on their age.
📋 What You'll Learn
Create a variable called age with a specific integer value.
Create a variable called voting_age set to 18.
Use the ternary operator to create a variable can_vote that stores the string 'Yes' if age is greater than or equal to voting_age, otherwise 'No'.
Print the value of can_vote.
💡 Why This Matters
🌍 Real World
Ternary operators are used in real-world PHP scripts to make quick decisions without writing long if-else statements.
💼 Career
Understanding ternary operators helps you write cleaner and shorter code, a valuable skill for PHP developers.
Progress0 / 4 steps
1
Create the age variable
Create a variable called age and set it to the integer 20.
PHP
Hint
Use $age = 20; to create the variable.
2
Create the voting_age variable
Create a variable called voting_age and set it to the integer 18.
PHP
Hint
Use $voting_age = 18; to create the variable.
3
Use the ternary operator to set can_vote
Use the ternary operator to create a variable called can_vote that stores the string 'Yes' if age is greater than or equal to voting_age, otherwise 'No'.
PHP
Hint
Use $can_vote = ($age >= $voting_age) ? 'Yes' : 'No'; to assign the value.
4
Print the result
Print the value of the variable can_vote using echo.
PHP
Hint
Use echo $can_vote; to print the result.
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
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 A
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
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 C
Quick Check:
Syntax = condition ? true : false [OK]
Hint: Remember: question mark before colon in ternary [OK]