0
0
PHPprogramming~10 mins

Enums 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 basic enum named Color.

PHP
<?php
enum [1] {
    case RED;
    case GREEN;
    case BLUE;
}
Drag options to blanks, or click blank then click option'
AColor
BColors
CCOLOR
Dcolor
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase enum name like 'color' which is not conventional.
Using plural form 'Colors' which is not required.
2fill in blank
medium

Complete 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'
AGreen
BgreenCase
Cgreen
DGREEN
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or camelCase which will cause errors.
Using plural or modified case names.
3fill in blank
hard

Fix 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'
Aclass
Binterface
Cenum
Dtrait
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' or 'interface' instead of 'enum'.
Forgetting the keyword entirely.
4fill in blank
hard

Fill 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'
Astring
B'active'
Cint
D'active_status'
Attempts:
3 left
💡 Hint
Common Mistakes
Using int type but assigning string values.
Not quoting string values.
5fill in blank
hard

Fill 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'
Acases
Bcase
C$status
D$case
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.