0
0
PHPprogramming~10 mins

Enum backed values in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a backed enum with string values.

PHP
<?php
enum Status: string {
    case Pending = [1];
    case Approved = 'approved';
    case Rejected = 'rejected';
}
Drag options to blanks, or click blank then click option'
Apending
B'pending'
C"pending"
DPending
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around string values
Using unquoted identifiers as values
2fill in blank
medium

Complete the code to get the backed value of the enum case.

PHP
<?php
enum Direction: string {
    case North = 'N';
    case South = 'S';
}

echo Direction::North->[1];
Drag options to blanks, or click blank then click option'
Avalue
Bname
CgetValue()
DtoString()
Attempts:
3 left
💡 Hint
Common Mistakes
Using methods like getValue() which do not exist
Using the name property instead of value
3fill in blank
hard

Fix the error in the code to get the enum case from a backed value.

PHP
<?php
enum Status: int {
    case Draft = 0;
    case Published = 1;
}

$status = Status::from([1]);
echo $status->name;
Drag options to blanks, or click blank then click option'
A'1'
B"Published"
C1
DPublished
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string instead of an integer to from()
Passing the case name instead of the backed value
4fill in blank
hard

Fill both blanks to create a backed enum and get a case from a backed value.

PHP
<?php
enum Level: [1] {
    case Low = 1;
    case High = 2;
}

echo Level::[2]->value;
Drag options to blanks, or click blank then click option'
Aint
BLow
CHigh
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using string as backing type when values are integers
Using the wrong case name
5fill in blank
hard

Fill all three blanks to define a backed enum, get a case from a value, and print its name.

PHP
<?php
enum Color: [1] {
    case Red = 'R';
    case Blue = 'B';
    case Green = 'G';
}

$color = Color::from([2]);
echo $color->[3];
Drag options to blanks, or click blank then click option'
Astring
B'B'
Cname
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using int as backing type when values are strings
Passing unquoted values for string backed enums
Using value property instead of name to print case name