1

I've been trying to debug this for the past five minutes, I just don't get what the problem is:

Here's my code, lines 33 - 37:

for($i = 0; $i < 5; $i++) {
    $followers_change[$i] = $en_array1[$i]['followers']-$en_array2[$i]['followers'];
    $rank_change[$i] = $en_array1[$i]['rank']-$en_array2[$i]['rank'];
        echo "<tr><td>$en_array1[$i]['rank']</td><td><img src='$en_array1[$i]['imageurl']' width='48' height='48'/></td><td>$en_array1[$i]['name']</td><td>$en_array1[$i]['followers]'</td><td>$en_array1['followers_change']</td></tr>";
}

I keep getting the error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /.../ on line 36

I apologize for this being such a stupid/rookie error, I'm just stumped and tired at the same time (which doesn't make a good combination :)).

2
  • I personally avoid using array subscripts inside strings because it makes the kind of error you've got harder to detect. Commented Aug 31, 2011 at 2:48
  • String interpolation in double quotes follows specific rules. Use the curly braces syntax when in doubt. Commented Aug 31, 2011 at 3:29

4 Answers 4

7

error in the followers key, and you should use this code:

  for($i = 0; $i < 5; $i++) {
      $followers_change[$i] = $en_array1[$i]['followers']-$en_array2[$i]['followers'];
      $rank_change[$i] = $en_array1[$i]['rank']-$en_array2[$i]['rank'];
          echo "<tr><td>" . $en_array1[$i]['rank'] . "</td><td><img src='" . $en_array1[$i]['imageurl'] . "' width='48' height='48'/></td><td>" . $en_array1[$i]['name'] . "</td><td>" . $en_array1[$i]['followers'] . "'</td><td>" . $en_array1['followers_change'] . "</td></tr>";
  }
Sign up to request clarification or add additional context in comments.

5 Comments

$en_array1[$i]['followers] it not valid
Thanks, failure to properly encapsulate + the followers key issue was the problem :)! I didn't realize that I'd have to encapsulate a variable to be honest, as there's times when a string seems to echo fine inclusive of variables without encapsulation, and no it seems times when it doesn't. Any idea why the latter might have occurred? Would it be because of src='$en_array1[$i]['followers']'?
You welcome, that is a good question. You are probably right, I'll have to dig into it to find why.
Why not using the echo "<tr><td>{$en_array1[$i]['rank']}...?
It's a personal preference, I think it's easier to read when you use the quotes and dots.
4

See :

 .... ><td>$en_array1[$i]['followers]'</td ...

Do you see the mismatched ' after followers] ?

Comments

4
<?

for($i = 0; $i < 5; $i++) {
    $followers_change[$i] = $en_array1[$i]['followers']-$en_array2[$i]['followers'];
    $rank_change[$i] = $en_array1[$i]['rank']-$en_array2[$i]['rank'];
        echo "<tr><td>{$en_array1[$i]['rank']}</td><td><img src='{$en_array1[$i]['imageurl']}' width='48' height='48'/></td><td>{$en_array1[$i]['name']}</td><td>{$en_array1[$i]['followers']}</td><td>{$en_array1['followers_change']}</td></tr>";
}

You're missing a ' and you should use {} around your vars

Comments

3

You should be either using {} to encapsulate your variables in that string or using . concatenation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.