1. <?php
    2. class Solution {
    3. public function isValid(string $s) {
    4. if (!$s) return true;
    5. $data = [')' => '(', '}' => '{', ']' => '['];
    6. $stack = new SplStack();
    7. for($i = 0; $i < strlen($s); $i++) {
    8. if (isset($data[$s[$i]])) {
    9. if ($stack->isEmpty()) {
    10. return false;
    11. }
    12. $elem = $stack->pop();
    13. if ($elem != $data[$s[$i]]) {
    14. return false;
    15. }
    16. } else {
    17. $stack->push($s[$i]);
    18. }
    19. }
    20. if (!$stack->isEmpty()) {
    21. return false;
    22. }
    23. return true;
    24. }
    25. }
    26. $s = '({})[]';
    27. $cls = new Solution();
    28. $ret = $cls->isValid($s);
    29. var_dump($ret);