📖 Scenario: You are building a simple system to manage user roles in a web application. Each user can have a role like Admin, Editor, or Viewer. Using enums helps keep these roles organized and easy to manage.
🎯 Goal: Create a PHP enum called UserRole with specific roles, then write code to check a user's role and print a message.
📋 What You'll Learn
Create a backed enum called UserRole with string values for Admin, Editor, and Viewer.
Create a variable called currentUserRole and assign it the UserRole::Editor value.
Write a match expression to print a message based on the currentUserRole.
Print the message to the screen.
💡 Why This Matters
🌍 Real World
Enums help organize fixed sets of related values like user roles, making code easier to read and less error-prone.
💼 Career
Understanding enums is useful for backend PHP development, especially in frameworks and applications that manage user permissions or states.
Progress0 / 4 steps
1
Create the UserRole enum
Create a backed enum called UserRole with string values: Admin = 'admin', Editor = 'editor', and Viewer = 'viewer'.
PHP
Hint
Use the enum keyword followed by the name UserRole and specify : string for backed enum. Then add case lines for each role.
2
Assign a role to currentUserRole
Create a variable called currentUserRole and assign it the value UserRole::Editor.
PHP
Hint
Use the enum name UserRole followed by ::Editor to assign the enum value to the variable.
3
Use match to create a message based on the role
Write a match expression that uses $currentUserRole to set a variable $message with these exact strings: UserRole::Admin => "You have full access.", UserRole::Editor => "You can edit content.", UserRole::Viewer => "You can view content."
PHP
Hint
Use the match expression with $currentUserRole and assign the result to $message. Match each enum case to the exact string.
4
Print the message
Write a print statement to display the $message variable.
PHP
Hint
Use print($message); to show the message on the screen.
Practice
(1/5)
1. What is the main purpose of using enum in PHP?
easy
A. To group related constant values under one name
B. To create a new type of variable that can hold any value
C. To define functions inside classes
D. To declare variables with dynamic types
Solution
Step 1: Understand what enums represent
Enums are used to group related constant values, like fixed options or states.
Step 2: Compare with other options
Other options describe unrelated concepts: variables or functions, not enums.
Final Answer:
To group related constant values under one name -> Option A
Quick Check:
Enums = group constants [OK]
Hint: Enums group fixed options clearly [OK]
Common Mistakes:
Thinking enums create dynamic variables
Confusing enums with classes or functions
Assuming enums can hold any value
2. Which of the following is the correct syntax to declare a backed enum with string values in PHP?
easy
A. enum Status: string { case Active = 'active'; case Inactive = 'inactive'; }
B. enum Status { case Active = 'active'; case Inactive = 'inactive'; }
C. enum Status: int { case Active = 'active'; case Inactive = 'inactive'; }
D. enum Status: string { Active = 'active'; Inactive = 'inactive'; }
Solution
Step 1: Identify backed enum syntax
Backed enums require a type after the enum name, like : string, and cases with assigned values.
Step 2: Check each option
enum Status: string { case Active = 'active'; case Inactive = 'inactive'; } correctly uses enum Status: string { case Active = 'active'; ... }. enum Status { case Active = 'active'; case Inactive = 'inactive'; } misses the type. enum Status: int { case Active = 'active'; case Inactive = 'inactive'; } uses wrong type int with string values. enum Status: string { Active = 'active'; Inactive = 'inactive'; } misses case keyword.
Final Answer:
enum Status: string { case Active = 'active'; case Inactive = 'inactive'; } -> Option A
Quick Check:
Backed enum syntax = enum Status: string { case Active = 'active'; case Inactive = 'inactive'; } [OK]
Hint: Backed enums need type and 'case' keyword [OK]
Common Mistakes:
Omitting the enum type after colon
Forgetting 'case' before each value
Assigning string values to int enums
3. What will be the output of the following PHP code?
enum Direction { case North; case South; case East; case West; } $dir = Direction::East; echo $dir->name;
medium
A. Error: Cannot access name
B. direction::East
C. East case
D. East
Solution
Step 1: Understand enum case name property
In PHP enums, $dir->name returns the case name as a string.
Step 2: Check the assigned case
The variable $dir is assigned Direction::East, so $dir->name is "East".
Final Answer:
East -> Option D
Quick Check:
Enum case name = East [OK]
Hint: Use ->name to get enum case name string [OK]
Common Mistakes:
Expecting the value instead of name
Trying to print enum case directly
Confusing case name with enum class name
4. Identify the error in this PHP enum declaration:
enum Color { case Red = 1; case Green = 2; case Blue; }
medium
A. Case Blue should have a value assigned
B. Missing enum type for backed enum values
C. Enum name must be lowercase
D. Cases cannot have integer values
Solution
Step 1: Check enum type for backed values
When enum cases have assigned values (like 1, 2), the enum must declare a type (e.g., : int).
Step 2: Analyze the given enum
The enum Color assigns values to some cases but does not specify : int after the enum name, causing a syntax error.
Final Answer:
Missing enum type for backed enum values -> Option B
Quick Check:
Backed enums need type declared [OK]
Hint: Backed enums must declare type after enum name [OK]
Common Mistakes:
Not declaring enum type when assigning values
Assuming cases can mix assigned and unassigned values
Thinking enum names must be lowercase
5. Given this enum:
enum Status: string { case Pending = 'pending'; case Approved = 'approved'; case Rejected = 'rejected'; }
Which code correctly converts a string $input to the matching Status enum case or returns null if no match?
hard
A. new Status($input)
B. Status::from($input)
C. Status::tryFrom($input)
D. Status::$input
Solution
Step 1: Understand enum methods for backed enums
PHP backed enums provide from() which throws error if no match, and tryFrom() which returns null if no match.
Step 2: Match requirement
The question asks to return matching enum or null if no match, so tryFrom() is correct.
Final Answer:
Status::tryFrom($input) -> Option C
Quick Check:
Safe enum conversion = tryFrom() [OK]
Hint: Use tryFrom() to safely convert string to enum [OK]
Common Mistakes:
Using from() which throws error on no match
Trying to instantiate enum with new
Accessing enum cases as static properties
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.