Challenge - 5 Problems
Enum Backed Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of enum backed value access
What is the output of this PHP code using backed enums?
PHP
<?php
enum Status: string {
case Pending = 'pending';
case Active = 'active';
case Archived = 'archived';
}
echo Status::Active->value;
Attempts:
2 left
💡 Hint
Access the string value of the enum case using ->value.
✗ Incorrect
In PHP backed enums, each case has a value property that holds its backed value. Here, Status::Active has the string 'active'.
❓ Predict Output
intermediate2:00remaining
Enum from method with backed values
What will this code output?
PHP
<?php
enum Level: int {
case Low = 1;
case Medium = 5;
case High = 10;
}
$level = Level::from(5);
echo $level->name;
Attempts:
2 left
💡 Hint
The from() method returns the enum case matching the value.
✗ Incorrect
Level::from(5) returns the enum case with value 5, which is Medium. Accessing ->name gives 'Medium'.
❓ Predict Output
advanced2:00remaining
What error does this enum code raise?
What error will this PHP code produce?
PHP
<?php
enum Color: string {
case Red = 'red';
case Green = 'green';
case Blue = 'blue';
}
$color = Color::from('yellow');
Attempts:
2 left
💡 Hint
from() throws an error if the value is not defined in the enum.
✗ Incorrect
The from() method throws a ValueError if the given value does not match any enum case.
🧠 Conceptual
advanced2:00remaining
Difference between from() and tryFrom() in backed enums
Which statement correctly describes the difference between from() and tryFrom() methods in PHP backed enums?
Attempts:
2 left
💡 Hint
One method is safer and returns null instead of error.
✗ Incorrect
from() throws a ValueError if no matching case is found; tryFrom() returns null instead of throwing.
❓ Predict Output
expert2:00remaining
Output of enum backed values in array map
What is the output of this PHP code?
PHP
<?php
enum Priority: int {
case Low = 1;
case Medium = 5;
case High = 10;
}
$values = array_map(fn($p) => $p->value, [Priority::Low, Priority::High]);
print_r($values);
Attempts:
2 left
💡 Hint
array_map extracts the ->value property from each enum case.
✗ Incorrect
The code maps each enum case to its backed int value, resulting in [1, 10].