3v4l.org

run code in 300+ PHP versions simultaneously
<?php enum Language: int { case English = 1; case SPANISH = 2; public static function getLanguageKey(string $language): int { $reflection = new \ReflectionEnum(Language::class); return $reflection->getCase($language)->getValue()->value; } public static function getLanguageName(int $languageKey): string { $reflection = new \ReflectionEnum(Language::class); $case = array_filter($reflection->getCases(), fn($enum) => $enum->getValue()->value === $languageKey); return $case[0]->getValue()->name; } } $input = 1; Language::getLanguageName($input); // 'English $input = 'English'; Language::getLanguageKey($input); // '1
Output for 8.1.3 - 8.1.34, 8.2.22 - 8.2.30, 8.3.5 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3

preferences:
53.4 ms | 721 KiB | 4 Q