0

I am trying to access a php array using something like this:

$ArrayName[$j-1]

The moment I type the -1, I am immediately notified of a syntax error.

Is it illegal to add/subtract a constant to/from the array counter in this way?

Here is a part of my code (skipped the SQL query, but the query works OK):

$result=$conn->query($query);

if(!$result) die ("Database access failed: ".$conn->error);
$rows=$result->num_rows;
$answerCount=3;
$totalDataCount=0;
while($totalDataCount<$rows){
for($j=$totalDataCount;$j<$answerCount;++$j){
  $result->data_seek($j);
  $row=$result->fetch_array(MYSQLI_ASSOC);
  $Answer[$j]=$row['answer_text'];
}
$Question4Answers=$row['Question_text'];
echo <<<_END

 <form action="#" method="post" name="enteranswer">
   <table width="400">
    <tr>
    <td><p>$Question4Answers</p></td>
    </tr>
    <tr>
    <td><label>
      <input type="radio" name="radioGroup1" value="answer1" >
      $Answer[$j-1]</label></td>
      </tr>
      <tr>
    <td><label>
      <input type="radio" name="radioGroup1" value="answer2" >
       $Answer[$j-2] </label></td>
      </tr>
  <tr>
    <td><label>
      <input type="radio" name="radioGroup1" value="answer3" >
      $Answer[$j-3] </label></td>
  </tr>
  </table>
   <input type="submit" name="radiosubmit" id="button1css"  value="Submit">
  </form>
   _END;
  $totalDataCount=$totalDataCount+3;
  $answerCount=$answerCount+3;
  }
}
Minaj
  • 165
  • 5

1 Answers1

1

Without seeing your error, it's difficult to be 100%, but it looks like this is a constraint of heredoc.

It looks like it should be

{$Answer[$j-3]}

This post explains

Calling PHP functions within HEREDOC strings

And the second example in php docs heredoc php

Community
  • 1
  • 1
Sundance.101
  • 404
  • 3
  • 10