3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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($braces, $closed[0]); 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)); ?>
based on VXq5d
Output for 7.0.30 - 7.3.0alpha1
Warning: strpos() expects parameter 1 to be string, array given in /in/v7n7O on line 56 Fatal error: Uncaught Error: Call to undefined function vardump() in /in/v7n7O:61 Stack trace: #0 /in/v7n7O(36): testValidity(Array, Array, '(', Array, Array) #1 /in/v7n7O(26): match(Array, Array, Array, Array) #2 /in/v7n7O(6): isValid(Array) #3 /in/v7n7O(94): braceCheck(Array) #4 {main} thrown in /in/v7n7O on line 61
Process exited with code 255.
Output for hhvm-3.22.0
Warning: strpos() expects parameter 1 to be string, array given in /in/v7n7O on line -1 Fatal error: Uncaught Error: Call to undefined function vardump() in /in/v7n7O:-1 Stack trace: #0 /in/v7n7O(-1): testValidity() #1 /in/v7n7O(-1): match() #2 /in/v7n7O(-1): isValid() #3 /in/v7n7O(-1): braceCheck() #4 {main}
Process exited with code 255.
Output for hhvm-3.18.5 - 3.21.3
Warning: strpos() expects parameter 1 to be string, array given in /in/v7n7O on line 56 Fatal error: Uncaught Error: Call to undefined function vardump() in /in/v7n7O:61 Stack trace: #0 /in/v7n7O(36): testValidity() #1 /in/v7n7O(26): match() #2 /in/v7n7O(6): isValid() #3 /in/v7n7O(94): braceCheck() #4 {main}
Process exited with code 255.
Output for 5.6.30
Parse error: syntax error, unexpected ':', expecting '{' in /in/v7n7O on line 2
Process exited with code 255.