Bird
Raised Fist0
PHPprogramming~10 mins

Enums in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Enums in PHP
Define enum with cases
↓
Use enum value in code
↓
Compare or switch on enum
↓
Get enum name or value
↓
End
This flow shows how you define an enum, use its values, compare or switch on them, and access their names or values.
Execution Sample
PHP
<?php
enum Status {
  case Pending;
  case Approved;
  case Rejected;
}

$current = Status::Approved;
if ($current === Status::Approved) {
  echo "Approved!";
}
?>
Defines an enum Status with three cases, sets current status to Approved, then checks and prints if it is Approved.
Execution Table
StepActionEvaluationResult
1Define enum Status with cases Pending, Approved, RejectedN/AEnum Status created
2Assign $current = Status::ApprovedN/A$current holds Status::Approved
3Check if $current === Status::Approved$current === Status::ApprovedTrue
4Execute echo "Approved!"Print outputApproved!
5End of scriptN/AScript ends
💡 Script ends after printing 'Approved!' because condition is true and no more code.
Variable Tracker
VariableStartAfter Step 2After Step 5
$currentundefinedStatus::ApprovedStatus::Approved
Key Moments - 3 Insights
Why do we use === instead of == to compare enum cases?
Enums are objects, so === checks if they are exactly the same case instance. Using == might not work as expected. See step 3 in execution_table.
Can enum cases have values like strings or numbers?
Yes, PHP enums can be backed by strings or integers, but this example uses pure enums without values. This example shows simple cases only.
What happens if the condition in step 3 is false?
The echo statement would not run, so nothing would print. The script would end silently.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $current after step 2?
AStatus::Pending
BStatus::Rejected
CStatus::Approved
Dundefined
💡 Hint
Check the variable_tracker row for $current after step 2.
At which step does the script print output?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the execution_table action that says 'Execute echo "Approved!"'.
If we changed $current to Status::Rejected, what would happen at step 3?
ACondition would be false
BError occurs
CCondition would be true
DScript prints 'Approved!' anyway
💡 Hint
Compare $current value with Status::Approved in step 3 condition.
Concept Snapshot
PHP Enums let you define named cases as a type.
Use enum cases with EnumName::CaseName.
Compare enum cases with === for exact match.
Enums can be pure or backed by values.
Use enums to make code clearer and safer.
Full Transcript
This example shows how to define an enum called Status with three cases: Pending, Approved, and Rejected. We assign the variable $current to Status::Approved. Then we check if $current is exactly Status::Approved using the === operator. Since it is true, the script prints 'Approved!'. The script then ends. Enums help group related values and make code easier to read and maintain. Remember to use === to compare enum cases because they are objects. If the condition was false, the echo would not run and nothing would print.

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

  1. Step 1: Understand what enums represent

    Enums are used to group related constant values, like fixed options or states.
  2. Step 2: Compare with other options

    Other options describe unrelated concepts: variables or functions, not enums.
  3. Final Answer:

    To group related constant values under one name -> Option A
  4. 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

  1. Step 1: Identify backed enum syntax

    Backed enums require a type after the enum name, like : string, and cases with assigned values.
  2. 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.
  3. Final Answer:

    enum Status: string { case Active = 'active'; case Inactive = 'inactive'; } -> Option A
  4. 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

  1. Step 1: Understand enum case name property

    In PHP enums, $dir->name returns the case name as a string.
  2. Step 2: Check the assigned case

    The variable $dir is assigned Direction::East, so $dir->name is "East".
  3. Final Answer:

    East -> Option D
  4. 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

  1. 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).
  2. 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.
  3. Final Answer:

    Missing enum type for backed enum values -> Option B
  4. 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

  1. 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.
  2. Step 2: Match requirement

    The question asks to return matching enum or null if no match, so tryFrom() is correct.
  3. Final Answer:

    Status::tryFrom($input) -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard