A
A
Anastasia2021-06-14 03:01:35
PHP
Anastasia, 2021-06-14 03:01:35

Is it possible to write variables with keys in a string?

Hello. I need to collect the HTML code as a string and send it to the client. I need to fill this code with variables. The problem is that the variables are from an array and they all have keys. That is, I need to specify variables with keys in the line.
This does not work:

foreach ($orders['entries'] as $o) {
$htmlorder[] = "<td class='id'>$o['_id']</td>";
}


The only option is to isolate the variables with quotes with dots? Or is there some other syntax?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2021-06-14
@nastya97core

Wrap with curly braces

foreach ($orders['entries'] as $o) {
$htmlorder[] = "<td class='id'>{$o['_id']}</td>";
}

L
Lander, 2021-06-14
@usdglander

There is another option without curly braces. But I would strongly advise against using it.

foreach ($orders['entries'] as $o) {
$htmlorder[] = "<td class='id'>$o[_id]</td>";
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question