3v4l.org

run code in 500+ PHP versions simultaneously
<?php // TimeFormat is only present to demonstrate a mistake. enum TimeFormat : string { case Short = 'concise'; case Long = 'full'; } // String 'long' and 'short' were the options before refactoring. // Always falling back to 'long' on something invalid. Never throwing or aborting the application. enum DateFormat : string { case Long = 'long'; case Abbreviated = 'short'; } function FormatCurrentDate($dateFormat = 'long') { // This should not throw. // // If a mistake is made, and somewhere a date time is presented in long format, // where a short format is wanted, that is not a reason to throw, aborting the application. // // The result of such a mistake is that in the browser maybe some text looks somewhat distorted. // Which is annoying, but not show stopping. $dateFormat = DateFormat::tryFrom($dateFormat) ?? DateFormat::Long; var_dump($dateFormat); } FormatCurrentDate('short'); // old style FormatCurrentDate(null); // sometimes the database contains NULL values FormatCurrentDate(); // maybe valid, maybe a mistake. It is unclear because no explicit value is given. More context is needed. FormatCurrentDate(DateFormat::Long); // new style FormatCurrentDate(TimeFormat::Short); // a mistake. But not a reason to throw, aborting the application. /* ------------------------------------------------ */ /* Not a reason to throw, aborting the application. */ /* ------------------------------------------------ */ $setting = new DateTimeImmutable(); // assigned somewhere else, in another .php file. The wrong setting was retrieved. FormatCurrentDate($setting); // maybe valid, maybe a mistake. Because $setting could be anything. /* ----------------------------------------------- */ /* Not a reason to assume $format is always valid. */ /* ------------------------------------------------ */ $setting = 'short'; // assigned somewhere else, in another .php file. FormatCurrentDate($setting); // maybe valid, maybe a mistake. Because $setting could be anything.
Output for 8.4.9 - 8.4.26, 8.5.0 - 8.5.11
enum(DateFormat::Abbreviated) Deprecated: DateFormat::tryFrom(): Passing null to parameter #1 ($value) of type string|int is deprecated in /in/Pmaph on line 28 enum(DateFormat::Long) enum(DateFormat::Long) Fatal error: Uncaught TypeError: DateFormat::tryFrom(): Argument #1 ($value) must be of type string|int, DateFormat given in /in/Pmaph:28 Stack trace: #0 /in/Pmaph(28): DateFormat::tryFrom(DateFormat::Long) #1 /in/Pmaph(36): FormatCurrentDate(DateFormat::Long) #2 {main} thrown in /in/Pmaph on line 28
Process exited with code 255.
Output for 8.2.34, 8.3.0 - 8.3.35
enum(DateFormat::Abbreviated) Deprecated: DateFormat::tryFrom(): Passing null to parameter #1 ($value) of type string|int is deprecated in /in/Pmaph on line 28 enum(DateFormat::Long) enum(DateFormat::Long) Fatal error: Uncaught TypeError: DateFormat::tryFrom(): Argument #1 ($value) must be of type string|int, DateFormat given in /in/Pmaph:28 Stack trace: #0 /in/Pmaph(28): DateFormat::tryFrom(Object(DateFormat)) #1 /in/Pmaph(36): FormatCurrentDate(Object(DateFormat)) #2 {main} thrown in /in/Pmaph on line 28
Process exited with code 255.

preferences:
35.11 ms | 1141 KiB | 9 Q