空行上的PHP语法错误:语法错误,意外'}'

时间:2017-06-13 06:11:28

标签: php

Parse error: syntax error, unexpected '}', expecting end of file

它出现在我的removeWalls函数和安装注释之间的空行,但我肯定没有丢失的分号或未闭合的括号。有谁知道为什么会这样,以及如何解决它?

$cols;
$rows;
$w = 20;
$grid = new jsarray();
$current;
$stack = new jsarray();
$height = 600;
$width = 600;
$cols = floor($width/$w);
$rows = floor($height/$w);

function index($i, $j) {
  if ($i < 0 || $j < 0 || $i > $cols - 1 || $j > $rows - 1) {
    return -1;
  }
  return $i + $j * $cols;
}

// Classes
class Cell{
  function __construct($i,$j){
    $this->i = $i;
    $this->j = $j;
    $this->walls = new jsarray(true,true,true,true);
    $this->visited = false;
  }
}

function removeWalls($a, $b) {
  $x = $a->i - $b->i;
  if ($x === 1) {
    $a->walls(3,false);
    $b->walls(1,false);
  } else if (x === -1) {
    $a->walls(1,false);
    $b->walls(3,false);
  }
  $y = $a->j - $b->j;
  if ($y === 1) {
    $a->walls(0,false);
    $b->walls(2,false);
  } else if (y === -1) {
    $a->walls(2,false);
    $b->walls(0<false);
  }
}

// Setup
for($j = 0;$j < $rows;$j++){
  for($i = 0;$i < $cols;$i++){
    $cell = new Cell($i,$j);
    $grid->push($cell);
  }
}
$current = $grid(0);

?>

JSarray Class:

<?php
Class jsarray{
  function __construct(...$vals_){
    $this->vals = array();
    $this->cnt = -1;
    if($vals_ != NULL){
      if($vals_ > 0 && $vals_ > 1){
        foreach($vals_ as $val){
          $this->vals[(string) $this->cnt] = $val;
          $this->cnt++;
        }
      }
    }
    $this->length = $this->cnt+1;
  }
  function push(...$elements){
    if($elements != NULL && $elements > 0){
      foreach($elements as $element){
        $this->vals[(string) $this->cnt] = $element;
        $this->cnt++;
      }
    }
    $this->length = $this->cnt+1;
  }
  function pop(){
    if($this->length > 0){
      array_pop($this->vals);
      $this->cnt--;
    }
    $this->length = $this->cnt+1;
  }
  function __invoke(int $indx,$exchange,...$vals0){
    if($indx != NULL && $indx > -1){
      return $this->vals[(string) ($indx-1)];
    } else if($exchange != NULL && $indx > -1) {
      $this->vals[(string) ($indx-1)] = $exchange;
    } else if($vals0 != NULL){
      $this->vals = array();
      $this->cnt = -1;
      $this->cnt = -1;
      if($vals_ > 0 && $vals_ > 1){
        foreach($vals_ as $val){
          $this->vals[(string) $this->cnt] = $val;
          $this->cnt++;
        }
      }
    }
  }
}
  function concat(self ...$arrays){
    if($arrays > 0 && $arrays != NULL){
      $finalarr = $this;
      foreach($arrays as $array){
        $finalarr->vals = array_merge_recursive($finalarr->vals,$array->vals);
      }
      $finalarr->length = count($finalarr->vals);
      $finalarr->cnt = $finalarr->length-1;
      return $finalarr;
    }
  }
  function reverse(){
    $this->vals = array_reverse($this->vals);
  }
}
// one $ for every line of this file
?>

如果有人有兴趣我试图将一些JS翻译成PHP,而JS和PHP都在这个页面上:https://github.com/AlexDvorak/PHP-Coding-Train

2 个答案:

答案 0 :(得分:2)

} else if (y === -1) {行上缺少$符号,应为} else if ($y === -1) {

$b->walls(0<false);也可能是$b->walls(0, false);

答案 1 :(得分:1)

removeWalls函数中存在一些错误。缺少$符号和无效参数 改变如下:

function removeWalls($a, $b) {
  $x = $a->i - $b->i;
  if ($x === 1) {
    $a->walls(3,false);
    $b->walls(1,false);
  } else if ($x === -1) { //<-------------Mark here
    $a->walls(1,false);
    $b->walls(3,false);
  }
  $y = $a->j - $b->j;
  if ($y === 1) {
    $a->walls(0,false);
    $b->walls(2,false);
  } else if ($y === -1) { //<-------------Mark here
    $a->walls(2,false);
    $b->walls(0,false); //<-------------Mark here
  }
}