3v4l.org

run code in 500+ PHP versions simultaneously
<?php echo '----- Backed integer enum -----'.PHP_EOL; enum DayOfWeekInt : int { case Monday = 1; case Tuesday = 2; case Wednesday = 3; case Thursday = 4; case Friday = 5; case Saturday = 6; case Sunday = 7; } $var = DayOfWeekInt::Monday; try { echo '--- intval($var) --'.PHP_EOL; echo intval($var); } catch (\Throwable $ex) { echo $ex; } echo PHP_EOL; try { echo '--- (int) $var ---'.PHP_EOL; echo (int) $var; } catch (\Throwable $ex) { echo $ex; } echo PHP_EOL; echo PHP_EOL; echo '----- Backed string enum -----'.PHP_EOL; enum DayofWeekString : string { case Monday = 'monday'; case Tuesday = 'tuesday'; case Wednesday = 'wednesday'; case Thursday = 'thursday'; case Friday = 'friday'; case Saturday = 'saturday'; case Sunday = 'sunday'; } $var = DayofWeekString::Monday; try { echo '--- strval($var) ---'.PHP_EOL; echo strval($var); } catch (\Throwable $ex) { echo $ex; } echo PHP_EOL; try { echo '--- (string) $var ---'.PHP_EOL; echo (string) $var; } catch (\Throwable $ex) { echo $ex; } echo PHP_EOL;
Output for 8.2.31, 8.3.0 - 8.3.31, 8.4.1 - 8.4.22, 8.5.0 - 8.5.7
----- Backed integer enum ----- --- intval($var) -- Warning: Object of class DayOfWeekInt could not be converted to int in /in/pfXH4 on line 18 1 --- (int) $var --- Warning: Object of class DayOfWeekInt could not be converted to int in /in/pfXH4 on line 26 1 ----- Backed string enum ----- --- strval($var) --- Error: Object of class DayofWeekString could not be converted to string in /in/pfXH4:48 Stack trace: #0 {main} --- (string) $var --- Error: Object of class DayofWeekString could not be converted to string in /in/pfXH4:56 Stack trace: #0 {main}

preferences:
56.64 ms | 786 KiB | 4 Q