在包含的html中使用php变量
我在寻找一个很像这个问题的东西,但有点不同 在我的情况下,我希望使用如下代码:在包含的html中使用php变量,php,include,var,Php,Include,Var,我在寻找一个很像这个问题的东西,但有点不同 在我的情况下,我希望使用如下代码: $somevar = 'a test'; include("file.html"); 和file.html将包含 <b>hello, this is {$somevar}</b> 你好,我是{$somevar} 问题是它只打印您好,这里是{$somevar} 如何让HTML读取包含文件中的变量?echo“你好,这是{$somevar}”; echo "<b>hello, t
$somevar = 'a test';
include("file.html");
和file.html将包含
<b>hello, this is {$somevar}</b>
你好,我是{$somevar}
问题是它只打印您好,这里是{$somevar}
如何让HTML读取包含文件中的变量?echo“你好,这是{$somevar}”;
echo "<b>hello, this is {$somevar}</b>";
或
你好,我是
或
你好,我是
或
你好,我是
您需要将变量定义程序包含在另一个要访问它的程序中。 例如:
假设test.html有$somevar。
在file.html中,
您需要回显它您好,这是任何不在
标记集中的内容,或通过eval()
传递的内容(不好的主意,不要这样做),都没有通过php解析器运行。@如果您有点倒转,请参阅SamuelCook'sanswer@Izkata-是的,你说得对。我没有引起足够的注意。我不确定。一定有人不喜欢它:/一定有人不太喜欢我。值得注意的是@paquettg说,你必须将file.html更改为file.phpy。你不需要更改文件扩展名。@nickb在默认的apache设置下,你可以
<b>hello, this is <?=$somevar?></b>
<b>hello, this is <?php echo $somevar; ?></b>
<b>hello, this is <?php print $somevar; ?></b>
Say test.html has $somevar.
in file.html you do,
<?php
include('test.html');
echo "<b>hello, this is $somevar</b>";
?>
<?php
include "stuff.php";
$somevar = "test";
?>
<html>
<body><p><?php echo($somevar); ?></p></body>
</html>