PHP nesting of an array element inside a string with curly braces

I would like to know what the advantage of using curly braces is in the following context:

$world["foo"] = "Foo World!"; echo "Hello, {$world["foo"]}.\n"; 

has the following form:

 $world["foo"] = "Foo World!"; echo "Hello, $world["foo"].\n"; 

In particular, how is it that curly braces are a possible ambiguity in this case (or similar cases)?

+6
source share
2 answers

The second example will not be analyzed. So, better first :)

Anyway, I prefer to use

 echo "Hello" . $world["foo"] . ".\n"; 

Because itโ€™s easier for me to read.

In addition, there is another way:

 $world["foo"] = "Foo World!"; echo "Hello, $world[foo].\n"; 

There are no attempts to use one or the other. you that you (or your team) like.

+5
source

See other answers for an explanation of the echo, but if you use heredoc, for example the following:

 echo <<<EOHTML <td>{$entity['name']}</td> EOHTML; 

You need curly braces to use the associative array correctly.

+3
source

Source: https://habr.com/ru/post/920155/


All Articles