0
0
PHPprogramming~15 mins

Ternary operator in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the Ternary Operator in PHP
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use echo $can_vote; to print the result.