在 PHP 中使用 heredoc 中的变量(SQL 实践)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11274354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use a variable within heredoc in PHP (SQL practice)
提问by Mathieu
I'm a newbie to PHP/SQL and I am trying to use a variable within a heredoc as I need to output a lot of text. I've only included the first sentence as it is enough to show the problem).
我是 PHP/SQL 的新手,我正在尝试在 heredoc 中使用变量,因为我需要输出大量文本。我只包含了第一句话,因为它足以说明问题)。
My problem is that within the heredoc, the variables (see below: $data['game_name]and $data['game_owner']) are not recognized as variables, but as plain text. How can I solve this?
我的问题是,在heredoc 中,变量(见下文:$data['game_name]和$data['game_owner'])不被识别为变量,而是纯文本。我该如何解决这个问题?
<?php
try
{
// I am connecting the the database base mysql 'test'
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$bdd = new PDO('mysql:host=localhost;dbname=test', 'root', '', $pdo_options);
// Then I read the data in the database 'video_dame'
$response = $bdd->query('SELECT * FROM video_game');
// Pour qu'elle soit visible à l'écran, on affiche
// chaque entrée une à une
while ($data = $response->fetch())
{
echo <<<'EX'
<p>Game: $data['game_name]<br/>
the owner of the game is $data['game_owner']
</p>
EX;
}
// I end the SQL request
$response->closeCursor();
}
catch (Exception $e)
{
die('Error: ' . $e->getMessage());
}
?>
回答by Bojangles
Your heredoc needs a little modification (because it's actually Nowdoc!):
您的 heredoc 需要稍作修改(因为它实际上是 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 becomeEX. The heredoc terminator must nothave any preceding whitespace. From the documentation:
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']}.
- Heredoc 标识符(与 nowdoc 不同)不能被引用。
'EX'需要成为EX. 继承人终止符不能有任何前面的空格。从文档:
需要注意的是,带有结束标识符的行不能包含其他字符,分号 (;) 除外。
您将 Nowdoc 与 heredoc 混淆了。
- 字符串中的复杂数据类型必须用 包围才能将
{}它们解析为变量。例如,$data['game_name']应该是{$data['game_name']}。
You're mixing up heredoc and nowdoc here. You want to use heredocand notNowdoc 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.
你在这里混淆了 heredoc 和 nowdoc。您想使用heredoc而不是Nowdoc,因为您的字符串中有变量。Heredocs 是“扩展的”双引号字符串,而 nowdocs 更类似于单引号字符串,因为变量不在 nowdoc 字符串中解析,而是在 heredoc 中。
Please read the documentation more carefully on these.
请更仔细地阅读有关这些的文档。

