3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php // WARNING: this is just a way to experiment with short closures and you SHOULD NOT write code like this echo '# PART 1 - Short closures may contain only 1 expression and nothing more'.PHP_EOL.PHP_EOL; // Two expressions: Syntax error (not catchable in this branch) // $test = fn($x) => { $y = 10; return $x + $y; }; function last(...$args) { // all expression results enter here return end($args); // only last one is returned } // unlike JavaScript where the last expression is returned, PHP returns a boolean $andOperator = fn($x) => ($y = 10) && $x + $y; var_dump($andOperator(7)); // PHP also doesn't have a comma operator like JS. In JS "a, b" will return b // $commaOperator = fn($x) => $y = 10, $x + $y; // Syntax error // end() only accepts references, so no luck yet: //$arrayEnd = fn($x) => end([$y = 10, $x + $y]); // Fatal error: Only variables can be passed by reference $multipleLines = fn($x) => last( $y = $x * 10, $z = $y + 15, $x + $y + $z // result of this line is returned from last() ); var_dump($multipleLines(7)); $moreComplex = fn($x) => last( $numbers = range(0, $x), $y = array_map(fn($z) => strlen($z), $numbers), array_sum($y) // result of this line is returned from last() ); var_dump($moreComplex(7)); echo PHP_EOL; echo '# PART 2 - Short closures bind by value, always'.PHP_EOL.PHP_EOL; function walkThisWay(Closure $closure) { // just an example $i = 0; while ($word = $closure()) { echo $word.PHP_EOL; $i++; if ($i > 3) { // length of $parameters echo 'NAH! it seems it\'s not really a reference'.PHP_EOL; break; } } echo PHP_EOL; } echo "## TEST 1 - not a reference:".PHP_EOL; $parameters = ['banana', 'terracotta', 'pie']; // we want to change $parameters, but what we have is a copy of it, arrays behave this way walkThisWay(fn() => array_shift($parameters)); echo "## TEST 2 - syntax error:".PHP_EOL; $parameters = ['banana', 'terracotta', 'pie']; // Error is not catchable in this branch //walkThisWay(fn() use (&$parameters) => array_shift($parameters)); echo PHP_EOL; echo "## TEST 3 - object:".PHP_EOL; $parameters = ['banana', 'terracotta', 'pie']; $wrapper = (object) ['parameters' => &$parameters]; // objects are not really references, but they know where all data is, a copy is not made // You could also use ArayObject instead of $wrapper walkThisWay(fn() => array_shift($wrapper->parameters)); echo "## TEST 4 - functional hack:".PHP_EOL; $parameters = ['banana', 'terracotta', 'pie']; function ref(&$reference, Closure $closure) { return function (...$args) use ($closure, &$reference) { $args[] = &$reference; // add a reference to $parameters in the arguments return $closure(...$args); }; } walkThisWay(ref($parameters, fn(&$parameters) => array_shift($parameters))); echo "## TEST 5 - good old functions (better use this version in real code):".PHP_EOL; $parameters = ['banana', 'terracotta', 'pie']; walkThisWay(function () use (&$parameters) { return array_shift($parameters); });

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).
Note: hhvm uses a JIT which only kicks in after a few runs. Since we run scripts only once, hhvm always runs slower.

VersionSystem time (s)User time (s)Memory (MiB)
7.3.40.0090.00514.57
7.3.30.0080.00814.56
7.3.20.0110.00515.45
7.3.10.0120.00715.44
7.3.00.0120.00715.42
7.2.170.0080.00814.72
7.2.160.0100.00614.74
7.2.150.0110.00815.62
7.2.140.0170.00615.61
7.2.130.0150.00715.60
7.2.120.0130.00715.63
7.2.110.0120.00915.58
7.2.100.0130.00815.62
7.2.90.0140.00815.55
7.2.80.0110.00715.65
7.2.70.0140.00815.68
7.2.60.0190.00815.59
7.2.50.0130.01115.59
7.2.40.0150.00815.65
7.2.30.0120.00815.67
7.2.20.0150.00715.55
7.2.10.0140.00815.60
7.2.00.0120.00715.57
7.1.280.0160.00614.41
7.1.270.0100.00714.39
7.1.260.0110.00614.49
7.1.250.0100.00714.48
7.1.240.0120.00915.10
7.1.230.0110.00914.99
7.1.220.0130.00815.00
7.1.210.0100.00714.95
7.1.200.0140.00514.92
7.1.190.0110.01115.09
7.1.180.0110.01014.90
7.1.170.0130.00615.00
7.1.160.0100.01014.96
7.1.150.0140.00914.97
7.1.140.0170.00714.91
7.1.130.0140.00715.02
7.1.120.0070.01115.13
7.1.110.0140.00415.11
7.1.100.0130.00915.07
7.1.90.0120.00814.99
7.1.80.0130.00615.15
7.1.70.0120.00614.83
7.1.60.0100.00815.02
7.1.50.0130.00315.04
7.1.40.0240.00814.55
7.1.30.0130.00914.57
7.1.20.0140.00614.56
7.1.10.0100.01214.60
7.1.00.0780.01014.50
7.0.330.0120.00714.64
7.0.320.0170.00614.74
7.0.310.0130.00614.71
7.0.300.0140.00814.81
7.0.290.0140.00814.82
7.0.280.0170.00714.73
7.0.270.0110.00814.69
7.0.260.0090.00814.66
7.0.250.0130.00514.65
7.0.240.0130.01014.79
7.0.230.0140.01114.66
7.0.220.0110.00714.68
7.0.210.0150.01214.71
7.0.200.0120.00814.61
7.0.190.0140.01014.68
7.0.180.0170.00814.22
7.0.170.0140.00614.29
7.0.160.0140.00814.32
7.0.150.0150.00814.17
7.0.140.0130.01114.18
7.0.130.0190.00914.26
7.0.120.0130.00914.31
7.0.110.0160.00914.41
7.0.100.0140.00814.13
7.0.90.0160.01014.38
7.0.80.0140.00914.34
7.0.70.0080.01014.31
7.0.60.0120.00714.31
7.0.50.0130.00814.38
7.0.40.0120.00914.38
7.0.30.0140.01014.39
7.0.20.0190.00714.25
7.0.10.0140.00614.29
7.0.00.0140.00814.38
hhvm-3.22.00.1190.18191.22
hhvm-3.21.30.1000.15288.94
hhvm-3.21.00.1080.17988.65
hhvm-3.20.20.1290.18289.54
hhvm-3.19.20.1360.17788.86
hhvm-3.18.50.1170.16187.26
hhvm-3.18.40.1190.17787.60
hhvm-3.17.30.1200.14486.63
hhvm-3.15.40.1240.16984.45
hhvm-3.13.20.1320.16095.64
hhvm-3.12.140.1240.17194.53
hhvm-3.11.10.1210.17186.30
hhvm-3.10.10.1120.15983.55
5.6.400.0120.01014.07
5.6.390.0150.00613.98
5.6.380.0130.00713.98
5.6.370.0170.00813.86
5.6.360.0160.00914.12
5.6.350.0160.01014.03
5.6.340.0180.00714.11
5.6.330.0180.00914.04
5.6.320.0100.01414.09
5.6.310.0170.00713.94
5.6.300.0160.00814.05
5.6.290.0160.00714.09
5.6.280.0150.01013.92
5.6.270.0100.01114.28
5.6.260.0140.00914.21
5.6.250.0150.01214.15
5.6.240.0170.00914.10
5.6.230.0130.00614.19
5.6.220.0130.00814.14
5.6.210.0150.00714.07
5.6.200.0140.01414.10
5.6.190.0110.01014.18
5.6.180.0150.01014.02
5.6.170.0140.00713.95
5.6.160.0170.01014.16
5.6.150.0130.00914.09
5.6.140.0120.01213.91
5.6.130.0130.01214.11
5.6.120.0100.01314.03
5.6.110.0170.00914.05
5.6.100.0130.01013.95
5.6.90.0180.00814.00
5.6.80.0190.01414.00
5.6.70.0100.01114.03
5.6.60.0130.01113.97
5.6.50.0140.01113.93
5.6.40.0110.00813.85
5.6.30.0140.01213.89
5.6.20.0100.01213.86
5.6.10.0150.01214.04
5.6.00.0130.01013.96
5.5.380.0120.00814.00
5.5.370.0180.00813.93
5.5.360.0150.00714.03
5.5.350.0140.01114.02
5.5.340.0180.00614.03
5.5.330.0100.01114.02
5.5.320.0150.00614.09
5.5.310.0130.00814.07
5.5.300.0130.00814.15
5.5.290.0150.00814.11
5.5.280.0140.01113.96
5.5.270.0160.00813.88
5.5.260.0130.00914.00
5.5.250.0200.00513.99
5.5.240.0170.00913.88
5.5.230.0090.01113.96
5.5.220.0150.00913.98
5.5.210.0140.00913.85
5.5.200.0150.00813.69
5.5.190.0160.00413.94
5.5.180.0180.00713.96
5.5.170.0150.00714.00
5.5.160.0160.00714.03
5.5.150.0120.01113.95
5.5.140.0160.00613.87
5.5.130.0180.01013.93
5.5.120.0160.00613.94
5.5.110.0160.01013.93
5.5.100.0110.01113.84
5.5.90.0160.00913.97
5.5.80.0150.00713.91
5.5.70.0140.01313.84
5.5.60.0100.00813.77
5.5.50.0140.01013.96
5.5.40.0160.00313.92
5.5.30.0150.00913.80
5.5.20.0130.01013.79
5.5.10.0140.00813.90
5.5.00.0090.01113.83
5.4.450.0110.00411.81
5.4.440.0130.00511.81
5.4.430.0120.00811.81
5.4.420.0100.00811.81
5.4.410.0110.00711.81
5.4.400.0140.00511.81
5.4.390.0110.00811.81
5.4.380.0140.00411.81
5.4.370.0120.00511.81
5.4.360.0090.00711.81
5.4.350.0110.00811.81
5.4.340.0140.00411.81
5.4.330.0120.00511.81
5.4.320.0070.00711.81
5.4.310.0110.00611.81
5.4.300.0050.01011.81
5.4.290.0150.00411.81
5.4.280.0140.00311.81
5.4.270.0130.00511.81
5.4.260.0110.00711.81
5.4.250.0120.00511.81
5.4.240.0080.00711.81
5.4.230.0100.00811.81
5.4.220.0100.00811.81
5.4.210.0100.00411.81
5.4.200.0130.00511.81
5.4.190.0080.00711.81
5.4.180.0130.00411.81
5.4.170.0090.00711.81
5.4.160.0110.00611.81
5.4.150.0100.00711.81
5.4.140.0140.00311.81
5.4.130.0100.00611.81
5.4.120.0130.00311.81
5.4.110.0100.00211.81
5.4.100.0120.00411.81
5.4.90.0100.00611.81
5.4.80.0110.00411.81
5.4.70.0080.00511.81
5.4.60.0070.00811.81
5.4.50.0110.00711.81
5.4.40.0130.00311.81
5.4.30.0100.00611.81
5.4.20.0080.00611.81
5.4.10.0050.01111.81
5.4.00.0070.00611.81
5.3.290.0070.00611.81
5.3.280.0110.00211.81
5.3.270.0080.00511.81
5.3.260.0080.00711.81
5.3.250.0090.00611.81
5.3.240.0100.00411.81
5.3.230.0110.00411.81
5.3.220.0110.00411.81
5.3.210.0100.00711.81
5.3.200.0110.00411.81
5.3.190.0050.01011.81
5.3.180.0090.00211.81
5.3.170.0110.00711.81
5.3.160.0090.00311.81
5.3.150.0130.00511.81
5.3.140.0130.00311.81
5.3.130.0090.00411.81
5.3.120.0040.01011.81
5.3.110.0040.00711.81
5.3.100.0060.00711.81
5.3.90.0070.00511.81
5.3.80.0090.00511.81
5.3.70.0060.00711.81
5.3.60.0080.00411.81
5.3.50.0100.00311.81
5.3.40.0070.00511.81
5.3.30.0080.00711.81
5.3.20.0070.00811.81
5.3.10.0060.00711.81
5.3.00.0090.00711.81
5.2.170.0050.00611.81
5.2.160.0100.00411.81
5.2.150.0100.00511.81
5.2.140.0060.00611.81
5.2.130.0080.00511.81
5.2.120.0070.00511.81
5.2.110.0070.00711.81
5.2.100.0100.00411.81
5.2.90.0070.00411.81
5.2.80.0070.00511.81
5.2.70.0070.00511.81
5.2.60.0060.00811.81
5.2.50.0090.00411.81
5.2.40.0070.00511.81
5.2.30.0050.01011.81
5.2.20.0080.00711.81
5.2.10.0060.00711.81
5.2.00.0070.00411.81
5.1.60.0050.00611.81
5.1.50.0080.00411.81
5.1.40.0080.00511.81
5.1.30.0080.00611.81
5.1.20.0090.00411.81
5.1.10.0090.00311.81
5.1.00.0080.00311.81
5.0.50.0050.00611.81
5.0.40.0040.00711.81
5.0.30.0060.00411.81
5.0.20.0060.00211.81
5.0.10.0070.00411.81
5.0.00.0070.00211.81
4.4.90.0050.00411.81
4.4.80.0040.00611.81
4.4.70.0060.00311.81
4.4.60.0060.00511.81
4.4.50.0050.00411.81
4.4.40.0020.00711.81
4.4.30.0030.00711.81
4.4.20.0050.00511.81
4.4.10.0070.00311.81
4.4.00.0050.00411.81
4.3.110.0060.00411.81
4.3.100.0030.00411.81
4.3.90.0040.00411.81
4.3.80.0030.00611.81
4.3.70.0040.00411.81
4.3.60.0040.00511.81
4.3.50.0050.00711.81
4.3.40.0040.00511.81
4.3.30.0030.00411.81
4.3.20.0050.00211.81
4.3.10.0020.00511.81
4.3.00.0040.00211.81