3v4l.org

run code in 500+ PHP versions simultaneously
<?php enum SpecialReturnValue { case NoReturn; case ReturnWithoutValue; case ReturnDollarThis; case ReturnVoid; case ReturnNever; } class ShouldTypeError { public $uniqueId; public function __construct($returnValue) { static $uniqueId = 0; $this->uniqueId = $uniqueId; $uniqueId++; if ($returnValue !== SpecialReturnValue::NoReturn) { if ($returnValue === SpecialReturnValue::ReturnWithoutValue) { return; } elseif ($returnValue === SpecialReturnValue::ReturnDollarThis) { return $this; } elseif ($returnValue === SpecialReturnValue::ReturnVoid) { return void; } elseif ($returnValue === SpecialReturnValue::ReturnNever) { return never; } return $returnValue; } } } const SOMECONST = 'Const hello'; enum SomeEnum { case Hearts; case Diamonds; case Clubs; case Spades; } function SomeFunction() { } $SomeFunctionClosure = function() { }; $SomeFunctionFirstClassCallable = SomeFunction(...); $someFunctionClosureFromCallabe = Closure::fromCallable('SomeFunction'); $arrowFunction = fn($a) => $a; $testcases = [ SpecialReturnValue::NoReturn, SpecialReturnValue::ReturnWithoutValue, SpecialReturnValue::ReturnDollarThis, new ShouldTypeError(SpecialReturnValue::NoReturn), SOMECONST, 'abc', 1, 1.23, null, true, false, ['a', 'b', 'c'], [6 => 'six', 7 => 'seven', 67 => 'six seven'], ['a' => 'a', 'b' => 'b', 'c' => 'c', 0 => 'Zero'], SpecialReturnValue::ReturnVoid, SpecialReturnValue::ReturnNever, SomeEnum::Spades, function() {}, $SomeFunctionClosure, $SomeFunctionFirstClassCallable, $someFunctionClosureFromCallabe, $arrowFunction, new DateTimeImmutable(), fopen('php://memory', 'w+'), ]; foreach($testcases as $testcase) { echo "--------------[ TESTCASE ]--------------\n"; var_dump($testcase); echo "\n"; try { $didReturn = new ShouldTypeError($testcase); if ($testcase === SpecialReturnValue::NoReturn) { echo "Success: without a return statement is always valid.\n"; } elseif ($testcase === SpecialReturnValue::ReturnWithoutValue) { echo "Success: a return statement without a value is always valid.\n"; } elseif ($testcase === SpecialReturnValue::ReturnDollarThis) { echo "Dubious: return \$this is dubious.\n"; echo "- it fullfills the return type, so it could be allowed.\n"; echo "- but returning anything from a constructor is nonsense, because it is discarded by \"new\".\n"; echo " As shown by the fourth testcase new SomeTypeError(SpecialReturnValue::NoReturn).\n"; } else { echo "Error: why is it not a return TypeError?\n"; } if ($didReturn instanceof ShouldTypeError) { echo "Created uniqueId ".$didReturn->uniqueId."\n"; } else { echo "Failed to new a ShouldTypeError.\n"; } } catch (Throwable $ex) { echo "Success: throwable: ".$ex->getMessage()."\n"; } }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.5.90.0060.00217.41
8.5.80.0030.00617.21
8.5.70.0130.00417.18
8.5.60.0040.00417.41
8.5.50.0050.00318.84
8.5.30.0120.00619.52
8.5.20.0200.00917.48
8.5.10.0050.00518.59
8.4.240.0050.00619.85
8.4.230.0130.00119.86
8.4.220.0120.01119.89
8.4.210.0100.01220.03
8.4.180.0130.01219.89
8.4.170.0050.00622.43
8.4.160.0190.00822.16
8.4.90.0120.00919.95
8.3.330.0090.01018.94
8.3.320.0110.01118.95
8.3.310.0050.00418.82
8.3.300.0070.00620.56
8.3.290.0050.00620.50
8.3.180.0110.01218.75
8.3.50.0060.00318.71
8.2.320.0150.00918.42
8.2.310.0110.01018.38

preferences:
56.86 ms | 740 KiB | 5 Q