3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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)) );
Output for 5.4.0 - 5.4.21
Parse error: syntax error, unexpected 'if' (T_IF) in /in/s4hMQ on line 35
Process exited with code 255.
Output for 5.3.0 - 5.3.27
Parse error: syntax error, unexpected T_IF in /in/s4hMQ on line 35
Process exited with code 255.