Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Sorting Enum

<?php

enum E {
    case A;
    case B;
    
    case D;
    case E;
}

print_r(E::cases());

enum E2 {
    case E;
    case D;
    case B;
    case A;
}

print_r(E2::cases());

enum E3: int {
    case A = 3;
    case B = 12;
    
    case D = 1;
    case E = 20;
}

$cases = E3::cases();
uasort($cases, fn ($a, $b) => $a->value <=> $b->value);
print_r($cases);

Enum cases are sorted, by default, with their definition order. The name of the case does not matter, just the position of definition within the enumeration.

When the order of definition cannot be changed, as per coding convention, the cases may be backed, and then, sorted at the last moment. Just know that the cases must be in a variable first.

See Also

PHP Error Messages

PHP Features

Last updated: 09 November 2026