3v4l.org

run code in 300+ PHP versions simultaneously
<?php function braceCheck(array $values) : array { for ($i = 0; $i <= count($values) - 1; $i++) { $braces = str_split($values[$i]); $values[$i] = isValid($braces); } return $values; } function isValid(array $braces) : string { $available = ['{' => '}', '(' => ')', '[' => ']']; $opened = []; $closed = []; for ($i = 0; $i <= count($braces) - 1; $i++) { if (in_array($braces[$i], array_keys($available))) { $opened[] = $braces[$i]; continue; } $closed[] = $braces[$i]; } return match($braces, $available, $opened, $closed); } function match($braces, array $available, array $opened, array $closed) : string { $last_open = $opened[count($opened) - 1]; $opened = array_values($opened); $closed = array_values($closed); if(!testValidity($braces, $available, $last_open, $opened, $closed)) { return 'NO'; } if (count($opened) > 1) { $result = match( $braces, $available, array_slice($opened, 0, -1), array_slice($closed, 1) ); } return 'YES'; } function testValidity($braces, array $available, string $last_open, array $opened, array $closed) : bool { $position = strpos($closed[0], $braces); echo $position; $arr = array_slice($braces, 0, $position); $flipped = array_flip($available); vardump($arr, $flipped); in_array($arr, $flipped[$closed[0]]); // check if not nested properly if ($available[$last_open] !== $closed[0]) { // check if the brackets are consecutive if ($available[$opened[0]] === $closed[0]) { return true; } return false; } // check if the brackets are not consecutive if ($available[$opened[0]] !== $closed[0]) { // check if nested properly if ($available[$last_open] === $closed[0]) { return true; } return false; } return true; } $values = ['{[()]}', '{[(]}', '{}()[]', '{]}[']; // YES, NO, YES, NO var_dump(braceCheck($values)); ?>

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.0170.00318.42
8.3.50.0120.00316.26
8.3.40.0070.00718.71
8.3.30.0090.00618.59
8.3.20.0000.00819.25
8.3.10.0070.00021.04
8.3.00.0030.00517.80
8.2.180.0080.00818.01
8.2.170.0120.00322.96
8.2.160.0030.01220.73
8.2.150.0080.00025.66
8.2.140.0070.00024.66
8.2.130.0080.00019.70
8.2.120.0040.00426.35
8.2.110.0030.00620.52
8.2.100.0040.01117.72
8.2.90.0040.00417.75
8.2.80.0040.00418.64
8.2.70.0040.00417.68
8.2.60.0000.00717.68
8.2.50.0000.00818.10
8.2.40.0000.00821.08
8.2.30.0000.00719.28
8.2.20.0000.00817.81
8.2.10.0040.00417.96
8.2.00.0080.00019.18
8.1.280.0100.01025.92
8.1.270.0040.00424.02
8.1.260.0000.00726.35
8.1.250.0040.00428.09
8.1.240.0070.00419.36
8.1.230.0040.00720.99
8.1.220.0040.00418.32
8.1.210.0040.00418.77
8.1.200.0050.00317.10
8.1.190.0050.00317.11
8.1.180.0050.00318.10
8.1.170.0030.00718.51
8.1.160.0000.00718.74
8.1.150.0000.00718.64
8.1.140.0040.00417.54
8.1.130.0040.00418.84
8.1.120.0030.00517.20
8.1.110.0000.00717.13
8.1.100.0040.00417.20
8.1.90.0040.00417.36
8.1.80.0000.00817.25
8.1.70.0040.00417.29
8.1.60.0040.00417.39
8.1.50.0000.00817.25
8.1.40.0030.00717.39
8.1.30.0000.00717.45
8.1.20.0050.00217.43
8.1.10.0050.00317.40
8.1.00.0040.00417.32
8.0.300.0080.00018.77
8.0.290.0000.00817.05
8.0.280.0030.00318.43
8.0.270.0030.00517.09
8.0.260.0040.00417.16
8.0.250.0040.00416.93
8.0.240.0050.00316.79
8.0.230.0080.00016.88
8.0.220.0040.00716.73
8.0.210.0030.00316.92
8.0.200.0030.00316.88
8.0.190.0070.00016.81
8.0.180.0000.00716.86
8.0.170.0030.00616.87
8.0.160.0000.00716.86
8.0.150.0080.00016.81
8.0.140.0000.00816.84
8.0.130.0000.00613.47
8.0.120.0030.00516.89
8.0.110.0000.00816.70
8.0.100.0000.00716.84
8.0.90.0040.00416.76
8.0.80.0070.01216.89
8.0.70.0040.00416.82
8.0.60.0040.00416.89
8.0.50.0000.00716.73
8.0.30.0100.00916.90
8.0.20.0110.00717.40
8.0.10.0000.00716.80
8.0.00.0110.01216.54
7.4.330.0020.00215.55
7.4.320.0000.00616.53
7.4.300.0030.00316.53
7.4.290.0070.00016.47
7.4.280.0000.00716.52
7.4.270.0040.00416.55
7.4.260.0100.00016.61
7.4.250.0000.00816.38
7.4.240.0040.00416.60
7.4.230.0030.00316.62
7.4.220.0000.00816.41
7.4.210.0080.00816.60
7.4.200.0040.00416.46
7.4.160.0080.00916.59
7.4.140.0100.00917.86
7.4.130.0120.01216.48
7.4.120.0110.00816.54
7.4.110.0100.00716.54
7.4.100.0070.01116.61
7.4.90.0150.00616.53
7.4.80.0030.01219.39
7.4.70.0070.01016.55
7.4.60.0150.00416.66
7.4.50.0030.01416.39
7.4.40.0090.00916.69
7.4.00.0070.00714.93
7.3.330.0050.00013.45
7.3.320.0000.00813.40
7.3.310.0000.00716.45
7.3.300.0040.00416.46
7.3.290.0040.00416.26
7.3.280.0080.01116.35
7.3.260.0100.01216.54
7.3.240.0100.00716.61
7.3.230.0120.00616.69
7.3.210.0070.01016.43
7.3.200.0160.00016.46
7.3.190.0070.01516.42
7.3.180.0090.00916.46
7.3.170.0090.00616.37
7.3.160.0090.01116.39
7.2.330.0070.01116.84
7.2.320.0090.01316.47
7.2.310.0140.01116.92
7.2.300.0070.01016.72
7.2.290.0090.00916.87
7.2.60.0680.01015.02
7.2.50.0780.01214.80
7.2.40.0660.00815.49
7.2.30.0770.00915.57
7.2.20.0700.00915.47
7.2.10.0730.00815.50
7.2.00.0880.01215.23
7.1.200.0040.00415.71
7.1.170.0830.01113.78
7.1.160.0640.01216.39
7.1.150.0750.01016.85
7.1.140.0840.01116.98
7.1.130.0930.00816.87
7.1.120.0760.01716.67
7.1.110.0910.01015.80
7.1.100.1000.01016.12
7.1.90.0630.00916.14
7.1.80.1010.01016.17
7.1.70.0650.01115.30
7.1.60.0770.01333.22
7.1.50.0790.01832.69
7.1.40.1030.02232.48
7.1.30.1140.01432.58
7.1.20.1060.01332.65
7.1.10.0730.00714.91
7.1.00.0990.00814.80

preferences:
61.2 ms | 401 KiB | 5 Q