-2

The variable $n is readed from a form:

<?php
$nume=$_POST['n']
for($i=1;$i<=n;$i++)
{
    for($j=1;$j<=n;$j++)
    {
      if($i==$j) $a[$i][$j]=0;
      else $a[$i][$j]=$i;
      echo $a[$i][$j]." ";
    }
  echo $a[$i][$j];
}
?>

Parse error: syntax error, unexpected 'for' (T_FOR) in C:\wamp\www\php test 11F\test.php on line 3

What's the error, the problem? It is my first project in php.

Dave
  • 3,073
  • 7
  • 20
  • 33
Ruben
  • 3
  • 1
  • 4

2 Answers2

1
<?php
$n = 20;
for($i=1;$i<=$n;$i++)
{
    for($j=1;$j<=$n;$j++)
    {
      if($i==$j) 
          $a[$i][$j]=0;
      else 
        $a[$i][$j]=$i;
          echo $a[$i][$j]." ";
    }
}
?>

I fixed these issues,

  1. n should be - $n

  2. You are trying access value of $a[$i][$j] form outside the second loop - Then $j value equal to $n+1 - but you can get only 1 to $n

Outside the two loops print the array this way then you can see what are the accessible keys

echo "<pre>";
print_r($a);
echo "</pre>";
Gayan
  • 2,845
  • 7
  • 33
  • 60
0

there is no semicolon after $_POST['n']. check below updated code

<?php
$nume=$_POST['n'];
for($i=1;$i<=$nume;$i++)
{
for($j=1;$j<=$nume;$j++)
{
  if($i==$j) $a[$i][$j]=0;
  else $a[$i][$j]=$i;
  echo $a[$i][$j]." ";
}
echo $a[$i][$j];
}
?>
Bhavik
  • 495
  • 2
  • 10