3v4l.org

run code in 500+ PHP versions simultaneously
<?php enum FormatOutput: string { case Text = '666777'; case Html = 'html'; } const SOMECONST = 'Const hello'; function SomeFunction() { } $SomeFunctionClosure = function() { }; $SomeFunctionFirstClassCallable = SomeFunction(...); $someFunctionClosureFromCallabe = Closure::fromCallable('SomeFunction'); $arrowFunction = fn($a) => $a; $testcases = [ 666777, '666777', FormatOutput::Text, // For testing purposes: let's see what every type does... SOMECONST, 'abc', -6, 7, -6.7, null, true, false, ['a', 'b', 'c'], [6 => 'six', 7 => 'seven', 67 => 'six seven'], ['a' => 'a', 'b' => 'b', 'c' => 'c', 0 => 'Zero'], function() {}, $SomeFunctionClosure, $SomeFunctionFirstClassCallable, $someFunctionClosureFromCallabe, $arrowFunction, new DateTimeImmutable(), fopen('php://memory', 'w+'), ]; foreach($testcases as $testcase) { echo '--------------[ TESTCASE ]--------------'."\n"; var_dump($testcase); echo "\n"; echo '--- tryFrom()'."\n"; try { $value = FormatOutput::tryFrom($testcase); if ($value !== null) { echo 'Value is '.$value->name.' with value '.$value->value.'.'."\n"; } else { echo 'Value is NULL.'."\n"; } } catch (Throwable $exception) { echo $exception::class, ': ', $exception->getMessage(), "\n"; } echo '--- from()'."\n"; try { $value = FormatOutput::from($testcase); if ($value !== null) { echo 'Value is '.$value->name.' with value '.$value->value.'.'."\n"; } else { echo 'Value is NULL.'."\n"; } } catch (Throwable $exception) { echo $exception::class, ': ', $exception->getMessage(), "\n"; } echo "\n"; echo "\n"; echo "\n"; }

preferences:
54.41 ms | 1369 KiB | 5 Q