3v4l.org

run code in 300+ PHP versions simultaneously
<?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).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0070.01418.48
8.3.50.0080.01018.39
8.3.40.0070.01018.79
8.3.30.0110.00418.83
8.3.20.0040.00421.73
8.3.10.0040.00721.94
8.3.00.0030.00621.91
8.2.180.0060.00916.88
8.2.170.0000.01422.96
8.2.160.0070.00720.55
8.2.150.0060.00324.18
8.2.140.0040.00424.66
8.2.130.0000.00826.16
8.2.120.0070.00419.01
8.2.110.0060.00320.47
8.2.100.0090.00317.91
8.2.90.0040.00417.75
8.2.80.0040.00420.64
8.2.70.0040.00417.75
8.2.60.0040.00417.50
8.2.50.0000.00817.93
8.2.40.0000.00819.37
8.2.30.0000.00720.43
8.2.20.0070.00318.23
8.2.10.0040.00419.45
8.2.00.0040.00418.10
8.1.280.0150.00625.92
8.1.270.0050.00324.66
8.1.260.0150.00426.35
8.1.250.0000.00828.09
8.1.240.0100.00020.79
8.1.230.0070.00418.99
8.1.220.0000.00917.78
8.1.210.0030.00618.77
8.1.200.0000.01017.24
8.1.190.0040.00417.22
8.1.180.0030.00618.10
8.1.170.0080.00017.62
8.1.160.0080.00018.84
8.1.150.0040.00418.94
8.1.140.0070.00018.99
8.1.130.0000.00720.33
8.1.120.0050.00317.46
8.1.110.0040.00417.51
8.1.100.0050.00517.52
8.1.90.0040.00817.56
8.1.80.0000.00817.41
8.1.70.0000.00717.43
8.1.60.0040.00417.63
8.1.50.0030.00517.53
8.1.40.0000.00817.55
8.1.30.0000.00817.71
8.1.20.0040.00417.70
8.1.10.0040.00417.52
8.1.00.0040.00417.62
8.0.300.0060.00318.77
8.0.290.0000.00716.88
8.0.280.0040.00418.48
8.0.270.0030.00317.35
8.0.260.0030.00318.51
8.0.250.0050.00317.11
8.0.240.0040.00417.09
8.0.230.0030.00316.98
8.0.220.0030.00517.01
8.0.210.0080.00017.00
8.0.200.0030.00317.09
8.0.190.0080.00017.11
8.0.180.0000.00717.04
8.0.170.0060.00317.09
8.0.160.0080.00016.94
8.0.150.0040.00416.92
8.0.140.0030.00516.96
8.0.130.0060.00013.45
8.0.120.0000.00816.94
8.0.110.0040.00416.83
8.0.100.0040.00416.96
8.0.90.0000.00816.95
8.0.80.0090.00617.02
8.0.70.0020.00516.79
8.0.60.0040.00416.80
8.0.50.0040.00416.83
8.0.30.0110.01117.35
8.0.20.0110.01117.26
8.0.10.0020.00516.96
8.0.00.0090.01116.89
7.4.330.0020.00216.79
7.4.320.0000.00716.56
7.4.300.0000.00716.56
7.4.290.0030.00316.57
7.4.280.0050.00316.51
7.4.270.0000.00716.50
7.4.260.0000.00816.54
7.4.250.0040.00416.52
7.4.240.0070.00016.58
7.4.230.0050.00216.54
7.4.220.0000.00716.52
7.4.210.0090.00816.67
7.4.200.0050.00316.41
7.4.130.0120.00616.65
7.4.120.0110.00716.61
7.4.110.0110.00816.52
7.4.100.0090.00916.79
7.4.90.0060.01216.68
7.4.80.0090.00919.39
7.4.70.0070.01016.56
7.4.60.0000.01616.64
7.4.50.0040.00816.59
7.4.40.0030.02116.65
7.3.330.0050.00013.22
7.3.320.0050.00013.23
7.3.310.0030.00316.10
7.3.300.0000.00715.96
7.3.290.0030.00316.08
7.3.280.0050.01116.19
7.3.260.0100.00916.20
7.3.240.0140.00516.31
7.3.230.0060.00916.18
7.3.210.0040.01716.19
7.3.200.0080.00816.32
7.3.190.0070.00716.19
7.3.180.0100.00716.21
7.3.170.0160.00016.17
7.3.160.0100.01016.29
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.330.0060.01216.61
7.2.320.0090.01516.50
7.2.310.0140.00316.20
7.2.300.0130.00316.54
7.2.290.0130.00616.40
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
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

preferences:
67 ms | 401 KiB | 5 Q