php PHP在EOT中插入变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5784637/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:29:02  来源:igfitidea点击:

PHP Inserting variable inside EOT

phpvariables

提问by Adrian M.

I want to insert a variable inside the EOT but is not working (I am new to php, maybe that's why). This code is part of a script, when I echo $usernamealone it shows the real name, but when I put it inside EOT is displaying plain text not the real name..

我想在 EOT 中插入一个变量但不起作用(我是 php 新手,也许这就是原因)。此代码是脚本的一部分,当我$username单独回显时,它显示真实姓名,但是当我将其放入 EOT 时,显示的是纯文本而不是真实姓名。

What am I doing wrong?

我究竟做错了什么?

$username=getUsername($ID);

echo <<<'EOT'

Some HTML code goes here

Hello $username, welcome back!

Some HTML code goes here

EOT;

回答by mario

You must leave out the single quotes here:

您必须在此处省略单引号:

echo <<<'EOT'

This denotes the 'nowdoc'variant, which doesn't interpolate variables.

这表示“nowdoc”变体,它不插入变量。

But you need the original "heredoc"syntax without quotes:

但是您需要不带引号的原始“heredoc”语法:

echo <<<EOT

回答by s4nch3z

$variable = 'text';
echo <<<EOT
Some {$variable} here
EOT;