An enum in PHP is a special type that lets you define a set of named values. It helps group related constants together, making code easier to read and safer.
Click to reveal answer
beginner
How do you define a basic enum in PHP?
Use the enum keyword followed by the enum name and a list of cases inside curly braces. For example:
enum Status { case Pending; case Approved; case Rejected; }
Click to reveal answer
intermediate
What is a backed enum in PHP?
A backed enum associates each case with a scalar value like a string or int. This lets you store or compare enum cases easily. Example:
enum Status: string { case Pending = 'pending'; case Approved = 'approved'; }
Click to reveal answer
intermediate
How do you get the scalar value of a backed enum case?
You access the value property of the enum case. For example:
Yes! Enums can have methods just like classes. This lets you add behavior related to the enum cases. For example:<br><pre>enum Status { case Pending; case Approved; function label() { return match($this) { self::Pending => 'Waiting', self::Approved => 'Done' }; } }</pre>
Click to reveal answer
Which keyword is used to define an enum in PHP?
Aenum
Bclass
Cinterface
Dconst
✗ Incorrect
The enum keyword is used to define enums in PHP.
What type of values can backed enums have in PHP?
AOnly strings
BOnly integers
CAny data type
DStrings or integers
✗ Incorrect
Backed enums in PHP can have either string or integer scalar values.
How do you access the scalar value of a backed enum case?
AUsing the <code>name</code> property
BUsing the <code>value</code> property
CUsing the <code>getValue()</code> method
DUsing the <code>toString()</code> method
✗ Incorrect
The scalar value of a backed enum case is accessed via the value property.
Can PHP enums have methods?
AOnly static methods are allowed
BNo, enums cannot have methods
CYes, enums can have methods
DOnly abstract methods are allowed
✗ Incorrect
PHP enums can have methods to add behavior related to the enum cases.
Which PHP version introduced enums?
APHP 8.1
BPHP 7.4
CPHP 8.0
DPHP 8.2
✗ Incorrect
Enums were introduced in PHP 8.1.
Explain what enums are in PHP and why they are useful.
Think about how enums help organize fixed sets of values.
You got /3 concepts.
Describe the difference between pure enums and backed enums in PHP.
Consider how backed enums store extra information.
You got /3 concepts.
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.