Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a basic enum named Color.
PHP
<?php
enum [1] {
case RED;
case GREEN;
case BLUE;
}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase enum name like 'color' which is not conventional.
Using plural form 'Colors' which is not required.
✗ Incorrect
The enum name should be Color with uppercase first letter as per convention.
2fill in blank
mediumComplete the code to access the GREEN case of the Color enum.
PHP
<?php
$green = Color::[1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or camelCase which will cause errors.
Using plural or modified case names.
✗ Incorrect
Enum cases are accessed using uppercase names exactly as declared, so GREEN is correct.
3fill in blank
hardFix the error in the enum declaration by completing the missing keyword.
PHP
<?php
[1] Color {
case RED;
case BLUE;
}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' or 'interface' instead of 'enum'.
Forgetting the keyword entirely.
✗ Incorrect
The keyword to declare an enum in PHP is enum.
4fill in blank
hardFill both blanks to create a backed enum with string values.
PHP
<?php enum Status: [1] { case ACTIVE = [2]; case INACTIVE = 'inactive'; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using int type but assigning string values.
Not quoting string values.
✗ Incorrect
Backed enums require specifying the type like string and assigning string values like 'active'.
5fill in blank
hardFill all three blanks to iterate over enum cases and print their names.
PHP
<?php foreach (Status::[1]() as [2]) { echo [3]->name . PHP_EOL; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name like 'case()' instead of 'cases()'.
Using variable names without $ sign.
Trying to access name as a method instead of property.
✗ Incorrect
Use cases() to get all enum cases, iterate with a variable like $case, and access the name property.