Page 1 of 1

Error in code.

Posted: Tue Nov 11, 2008 2:16 pm
by afbase
I'm shaking off some rust in my php code for some math homework.
I get this problem when i run the code.
Parse error: syntax error, unexpected '[' in /home/clinton/test.php on line 30
This error corresponds to this line:

Code: Select all

q[$i][x]=p[$i-1][x];
[/b]
in the following code

Code: Select all

<?
//Vectors
$p[0][x] = 0;
$p[0][y] = 0;
 
$p[1][x] = 1;
$p[1][y] = 2;
 
$p[2][x] = 2;
$p[2][y] = 2;
 
$p[3][x] = 3;
$p[3][y] = 0;
 
//knot vectors
$knot = array (0,0,0,0,1,1,1,1);
$j = 1;
$t_prime = 1/2;
//division
for ($i=0; $i == 4; $i++){
if ($i <= $j - 1){
$q[$i][x]=$p[$i][x];
$q[$i][y]=$p[$i][y];
}
else if ($i>= $j && $i<=$j+2){
$q[$i][x]=(1-(($t_prime - $knot[$i-2])/($knot[$i+1]-$knot[$i-2])))*$p[$i-1][x]+(($t_prime - $knot[$i-2])/($knot[$i+1]-$knot[$i-2]))*$p[$i][x]; 
$q[$i][y]=(1-(($t_prime - $knot[$i-2])/($knot[$i+1]-$knot[$i-2])))*$p[$i-1][y]+(($t_prime - $knot[$i-2])/($knot[$i+1]-$knot[$i-2]))*$p[$i][y];
}
else if($i>=$j+3){
q[$i][x]=p[$i-1][x];
q[$i][y]=p[$i-1][y];
 
}
else();
}
 
print_r($q);
 
 ?>
I'm using vim as my IDE, but I think my rust is overwhelming. Thanks for any help :D

Re: Error in code.

Posted: Tue Nov 11, 2008 2:26 pm
by Mark Baker
string values that are array indexes should be quoted, and you need to use a dollar sign to indicate variables as well:
so

Code: Select all

q[$i][x]=p[$i-1]'x];
becomes

Code: Select all

$q[$i]['x']=$p[$i-1]['x'];

Re: Error in code.

Posted: Tue Nov 11, 2008 2:56 pm
by afbase
thanks