Use variable within heredoc in PHP (SQL practice)

穿精又带淫゛_ 提交于 2019-11-27 20:03:03
Bojangles

Your Heredoc needs a little modification (because it's actually Nowdoc!):

    echo <<<EX
    <p>Game: {$data['game_name']}<br/>
    the owner of the game is {$data['game_owner']}
    </p>
EX;
  • Heredoc identifiers (unlike nowdoc ones) cannot be quoted. 'EX' needs to become EX.
  • The Heredoc terminator must not have any preceding whitespace. From the docs:

    It is very important to note that the line with the closing identifier must contain no other characters, except possibly a semicolon (;).

    You're confusing Nowdoc with Heredoc.

  • Complex data types in strings must be surrounded by {} for them to be parsed as variables. For example, $data['game_name'] should be {$data['game_name']}.

You're mixing up heredoc and nowdoc here. You want to use Heredoc and not Nowdoc because you've got variables inside your string. Heredocs are "extended" double quoted strings, whereas nowdocs are more akin to a single quoted string, in that variables are not parsed in nowdoc strings, but are in heredoc.

  • More on Heredoc here.
  • More on Nowdoc here.

Please read the documentation more carefully on these.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!