2

Is there a way to call an array member inside the PHP HEREDOC syntax?

I understand the HEREDOC can take $strings, and that works. I tried calling array members, but that failed:

echo <<<EOF
<tr>
<td>$inner_array_member["ID"]</td>
<td>$inner_array_member["Date"]</td>
<td>$inner_array_member["Time"]</td>
<td>$inner_array_member["Published"]</td>
<td>$inner_array_member["Article"]</td>
<td><a href="rendered.php?editarticle=$inner_array_member["ID"]">EDIT</a></td>
</tr>
EOF;

However, if I rename the strings into simple form ($Article = $inner_array_member["Article"]), and call them, all is fine. Is there any way around this?

4
  • Have you tried reading the manual? Commented Aug 5, 2014 at 9:57
  • 1
    Yes, tho I didn't get it. And to be honest, the link you gave below also wasn't clear to me. Commented Aug 5, 2014 at 11:01
  • I gave two links. Not a problem if it is not clear yet (wasn't clear to me in the beginning either), you should train yourself to be used to the manual. It holds all the knowledge that you need, you just need to learn to extract it. Commented Aug 5, 2014 at 11:10
  • I am aware of that, however with no programming background, the questions I ask might often seem silly (as they do, it seems). The big problem in breaching any area of expertise is comprehension of problems and terminology. Lacking those two makes doing stuff difficult. (thanks) Commented Aug 5, 2014 at 11:26

3 Answers 3

6

Read the manual please before asking a question. It clearly states:

Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped, but the escape codes listed above can still be used. Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings.

After checking double-quoted strings, you get a link to string parsing. You take some time to read it, and you find out you have two ways:

echo <<<EOF
<tr>
<td>$inner_array_member[ID]</td>
<td>{$inner_array_member["ID"]}</td>
</tr>
EOF;

I recommend the latter, {$inner_array_member["ID"]}, it is more readable and few people recognize the first form (including me before reading about it last week). First form is also more limited.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I was quite tangled with HEREDOC!
2

try this

echo <<<EOF
<tr>
<td>{$inner_array_member["ID"]}</td>
<td>{$inner_array_member["Date"]}</td>
<td>{$inner_array_member["Time"]}</td>
<td>{$inner_array_member["Published"]}</td>
<td>{$inner_array_member["Article"]}</td>
<td><a href="rendered.php?editarticle={$inner_array_member["ID"]}">EDIT</a></td>
</tr>
EOF;

Comments

1

Use curly braces, like {$inner_array_member['Published']}

1 Comment

Please consider writing a line while downvoting

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.