0
0
PHPprogramming~10 mins

Enums in PHP - Step-by-Step Execution

Choose your learning style9 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.