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.

preferences:
21.79 ms | 474 KiB | 4 Q