意外的T_STRING错误 (PHP)


Unexpected T_STRING error (PHP)

我通常可以轻松捕捉这些,但是......

function linear_regression($x, $y) {
// calculate number points
$n = count($x);
// ensure both arrays of points are the same size
if ($n != count($y)) {
trigger_error("linear_regression(): Number of elements in coordinate arrays do not match.", E_USER_ERROR);
}
// calculate sums
$x_sum = array_sum($x);
$y_sum = array_sum($y);
$xx_sum = 0;
$xy_sum = 0;
for($i = 0; $i < $n; $i++) {
$xy_sum+=($x[$i]*$y[$i]);
$xx_sum+=($x[$i]*$x[$i]);
  }

  // calculate slope
  //$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / (($n * $xx_sum) - ($x_sum * $x_sum));
  $divisor = (($n * $xx_sum) – ($x_sum * $x_sum));
if ($divisor == 0){
 $m = 0;
} else {
$m = (($n * $xy_sum) – ($x_sum * $y_sum)) / $divisor;
}

  // calculate intercept
  $b = ($y_sum - ($m * $x_sum)) / $n;
  // return result
  return array("m"=>$m, "b"=>$b);
}
var_dump( linear_regression(array(1, 2, 3, 4, 4), array(1.5, 1.6, 2.1, 3.0, 6)) );

错误发生在这里$divisor = (($n * $xx_sum) – ($x_sum * $x_sum));

知道为什么吗?

号是一个花哨的 unicode 破折号(我认为是 em 破折号)而不是常规的 ascii 字符。

不要问我是怎么发现的,但你的"-"不是真正的"-",是另一个字符,但具有相同的外观

好吧,你没有问我,但我在 http://writecodeonline.com/php/发现,它无法识别你的"-"字符

现在使用真正的"-"工作代码,您可以复制粘贴,并查看:

function linear_regression($x, $y) {
// calculate number points
$n = count($x);
// ensure both arrays of points are the same size
if ($n != count($y)) {
trigger_error("linear_regression(): Number of elements in coordinate arrays do not match.", E_USER_ERROR);
}
// calculate sums
$x_sum = array_sum($x);
$y_sum = array_sum($y);
$xx_sum = 0;
$xy_sum = 0;
for($i = 0; $i < $n; $i++) {
$xy_sum+=($x[$i]*$y[$i]);
$xx_sum+=($x[$i]*$x[$i]);
  }

  // calculate slope
  //$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / (($n * $xx_sum) - ($x_sum * $x_sum));
  $divisor = (($n * $xx_sum) - ($x_sum * $x_sum));
if ($divisor == 0){
 $m = 0;
} else {
$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / $divisor;
}

  // calculate intercept
  $b = ($y_sum - ($m * $x_sum)) / $n;
  // return result
  return array("m"=>$m, "b"=>$b);
}
var_dump( linear_regression(array(1, 2, 3, 4, 4), array(1.5, 1.6, 2.1, 3.0, 6)) );

我像这样重新输入了有问题的行:

$divisor = (($n*$xx_sum)-($x_sum * $x_sum));

错误消失了。经过一番戳,错误似乎与中间的减号有关。删除它并重新输入它似乎可以解决问题。

看到这个小提琴:

Adam 是对的,但是,你也没有关闭for循环。在这里试试这个。

此外,减号在这里是错误的符号:

$divisor = (($n * $xx_sum) - ($x_sum * $x_sum));

以及这里:

$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / $divisor;

我清理了您的函数的版本,其中的闭合for循环也在这里到位。

function linear_regression($x, $y) {
  // calculate number points
  $n = count($x);
  // ensure both arrays of points are the same size
  if ($n != count($y)) {
    trigger_error("linear_regression(): Number of elements in coordinate arrays do not match.", E_USER_ERROR);
  }
  // calculate sums
  $x_sum = array_sum($x);
  $y_sum = array_sum($y);
  $xx_sum = 0;
  $xy_sum = 0;
  for($i = 0; $i < $n; $i++) {
    $xy_sum+=($x[$i]*$y[$i]);
    $xx_sum+=($x[$i]*$x[$i]);
    // calculate slope
    //$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / (($n * $xx_sum) - ($x_sum * $x_sum));
    $divisor = (($n * $xx_sum) - ($x_sum * $x_sum));
    if ($divisor == 0) {
      $m = 0;
    }
    else {
      $m = (($n * $xy_sum) - ($x_sum * $y_sum)) / $divisor;
    }
    // calculate intercept
    $b = ($y_sum - ($m * $x_sum)) / $n;
  } 
  // return result
  return array("m"=>$m, "b"=>$b);
} // linear_regression