3v4l.org

run code in 300+ PHP versions simultaneously
<?php const SOMECONST = 'Const hello'; enum SomeEnum { case Hearts; case Diamonds; case Clubs; case Spades; } function SomeFunction() { } $SomeFunctionClosure = function() { }; $SomeFunctionFirstClassCallable = SomeFunction(...); $someFunctionClosureFromCallabe = Closure::fromCallable('SomeFunction'); class ShouldTypeError { public $uniqueId; public function __construct($returnValue) { static $uniqueId = 0; $this->uniqueId = $uniqueId; $uniqueId++; if ($returnValue !== 'no-return') { if ($returnValue === '$this') { return $this; } elseif ($returnValue === 'no-return-value') { return; } elseif ($returnValue === 'void') { return void; } elseif ($returnValue === 'never') { return never; } return $returnValue; } } } $testcases = [ 'no-return', 'no-return-value', '$this', new ShouldTypeError('no-return'), SOMECONST, 'abc', 1, 1.23, null, true, false, ['a', 'b', 'c'], ['a' => 'a', 'b' => 'b', 'c' => 'c', 0 => 'Zero'], 'never', 'void', SomeEnum::Spades, function() { echo 'Hi'.PHP_EOL; }, $SomeFunctionClosure, $SomeFunctionFirstClassCallable, $someFunctionClosureFromCallabe, new DateTimeImmutable(), fopen('php://memory', 'w+'), ]; foreach($testcases as $testcase) { echo "--------------[ TESTCASE ]--------------\n"; var_dump($testcase); echo "\n"; try { $didReturn = new ShouldTypeError($testcase); switch($testcase) { case 'no-return': echo "Success: without a return statement is always valid.\n"; break; case 'no-return-value': echo "Success: a return statement without a value is always valid.\n"; break; case '$this': 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.\n"; echo " As shown by the fourth testcase new SomeTypeError('no-return').\n"; break; default: echo "Error: why is it not a return TypeError?\n"; break; } 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.20.0120.00917.66
8.5.10.0120.00816.75
8.4.170.0110.01320.60
8.4.160.0150.00820.53
8.4.90.0110.01218.22
8.3.300.0120.01118.66
8.3.290.0170.00818.89
8.3.180.0100.00817.04
8.3.50.0090.01117.04

preferences:
30.21 ms | 403 KiB | 5 Q