3v4l.org

run code in 300+ PHP versions simultaneously
<?php function braceCheck($values) { for ($i = 0; $i <= count($values) - 1; $i++) { $braces = str_split($values[$i]); $values[$i] = isValid($braces); } return $values; } function isValid($braces) { $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($available, $opened, $closed); } function match($available, $opened, $closed) { $last_open = $opened[count($opened) - 1]; $opened = array_values($opened); $closed = array_values($closed); if (count($opened) !== count($closed)) { echo 'count off'; return 'NO'; } // Check for invalid nesting or non-consecutive braces if(!testValidity($available, $last_open, $opened, $closed)) { return 'NO'; } if (count($opened) >= 1) { $result = match( $available, array_slice($opened, 0, -1), array_slice($closed, 1) ); } return 'YES'; } function testValidity($available, $last_open, $opened, $closed) { if ($available[$last_open] !== $closed[0]) { if ($available[$opened[0]] === $closed[0]) { return true; } return false; } return true; } $values = ['{[()]}', '{[(]}', '{}()[]', '{]}[']; var_dump(braceCheck($values)); ?>
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Parse error: syntax error, unexpected token "," in /in/NNUHJ on line 24
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Parse error: syntax error, unexpected token "," in /in/NNUHJ on line 24
Process exited with code 255.
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Notice: Undefined offset: -1 in /in/NNUHJ on line 29 Notice: Undefined index: in /in/NNUHJ on line 57 Notice: Undefined offset: 0 in /in/NNUHJ on line 57 count off Notice: Undefined offset: -1 in /in/NNUHJ on line 29 Notice: Undefined index: in /in/NNUHJ on line 57 Notice: Undefined offset: 0 in /in/NNUHJ on line 57 array(4) { [0]=> string(3) "YES" [1]=> string(2) "NO" [2]=> string(3) "YES" [3]=> string(3) "YES" }
Output for 7.3.32 - 7.3.33
count offarray(4) { [0]=> string(3) "YES" [1]=> string(2) "NO" [2]=> string(3) "YES" [3]=> string(3) "YES" }

preferences:
208.05 ms | 401 KiB | 287 Q