PHP Enums

Define strictly-typed enumerations with backed values in PHP 8.1+.

PHP 8.1 introduced native Enums. Backed enums can have string or integer values, eliminating brittle string constants throughout your application code.

Key Takeaways: PHP Enums

Mastering PHP Enums helps you write clean and reliable PHP code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

PHP Enums Code Example

PHP
enum OrderStatus: string {
    case Pending = "pending";
    case Paid = "paid";
    case Cancelled = "cancelled";

    public function label(): string {
        return match($this) {
            self::Pending => "Awaiting Payment",
            self::Paid => "Order Completed",
            self::Cancelled => "Order Voided",
        };
    }
}

echo OrderStatus::Paid->label();
Output
Order Completed
Run this example on your own computer. The "Get Started" lesson shows how to set it up.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services