3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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(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($available, $opened, $closed); } function match(array $available, array $opened, array $closed) : string { $last_open = $opened[count($opened) - 1]; $opened = array_values($opened); $closed = array_values($closed); if (count($opened) !== count($closed)) { 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(array $available, string $last_open, array $opened, array $closed) : bool { // check nesting if ($available[$last_open] !== $closed[0]) { // check consecutive if ($available[$opened[0]] === $closed[0]) { return true; } return false; } return true; } $values = ['{[()]}', '{[(]}', '{}()[]', '{]}[']; var_dump(braceCheck($values)); ?>
based on NNUHJ
Output for 7.0.30 - 7.3.0beta1
Notice: Undefined offset: -1 in /in/n2Uia on line 31 Fatal error: Uncaught TypeError: Argument 2 passed to testValidity() must be of the type string, null given, called in /in/n2Uia on line 41 and defined in /in/n2Uia:57 Stack trace: #0 /in/n2Uia(41): testValidity(Array, NULL, Array, Array) #1 /in/n2Uia(49): match(Array, Array, Array) #2 /in/n2Uia(49): match(Array, Array, Array) #3 /in/n2Uia(49): match(Array, Array, Array) #4 /in/n2Uia(25): match(Array, Array, Array) #5 /in/n2Uia(5): isValid(Array) #6 /in/n2Uia(72): braceCheck(Array) #7 {main} thrown in /in/n2Uia on line 57
Process exited with code 255.
Output for hhvm-3.22.0
Notice: Undefined index: -1 in /in/n2Uia on line -1 Fatal error: Uncaught TypeError: Argument 2 passed to testValidity() must be an instance of string, null given in /in/n2Uia:-1 Stack trace: #0 /in/n2Uia(-1): testValidity() #1 /in/n2Uia(-1): match() #2 /in/n2Uia(-1): match() #3 /in/n2Uia(-1): match() #4 /in/n2Uia(-1): match() #5 /in/n2Uia(-1): isValid() #6 /in/n2Uia(-1): braceCheck() #7 {main}
Process exited with code 255.
Output for hhvm-3.18.5 - 3.21.3
Notice: Undefined index: -1 in /in/n2Uia on line 31 Fatal error: Uncaught TypeError: Argument 2 passed to testValidity() must be an instance of string, null given in /in/n2Uia:68 Stack trace: #0 /in/n2Uia(41): testValidity() #1 /in/n2Uia(50): match() #2 /in/n2Uia(50): match() #3 /in/n2Uia(50): match() #4 /in/n2Uia(25): match() #5 /in/n2Uia(5): isValid() #6 /in/n2Uia(72): braceCheck() #7 {main}
Process exited with code 255.
Output for 5.6.30
Parse error: syntax error, unexpected ':', expecting '{' in /in/n2Uia on line 11
Process exited with code 255.