2012-11-14 42 views
2

我在寻找类似Using PHP variables inside HTML tags?的问题,但有点不同。在包含的html中使用php变量

在我的情况,我想使用的代码矿这样的:

$somevar = 'a test'; 

include("file.html"); 

和file.html将包含

<b>hello, this is {$somevar}</b> 

的问题是,它只是打印hello, this is {$somevar}

如何让HTML读取包含文件中的变量?

+0

你需要呼应吧'你好,这是' –

+0

任何不在''标记集内,或通过'eval()'(坏主意,不要这样做)的东西,都不会通过php解析器运行。 –

+1

@relentless你有点倒退,看到SamuelCook的回答 – Izkata

回答

1
echo "<b>hello, this is {$somevar}</b>"; 

<b>hello, this is <?=$somevar?></b> 

<b>hello, this is <?php echo $somevar; ?></b> 

<b>hello, this is <?php print $somevar; ?></b> 
+0

为什么这是downvoted? – Ascherer

+0

im不确定。有人不得不喜欢它:/ –

+0

完美答案大声笑 – Ascherer

0

您需要包括变量定义程序,在其他程序想要访问它。 ? 例子:

Say test.html has $somevar. 
    in file.html you do, 

    <?php 
    include('test.html'); 
    echo "<b>hello, this is $somevar</b>"; 
    ?> 
0
<?php 
include "stuff.php"; 
$somevar = "test"; 
?> 

<html> 
    <body><p><?php echo($somevar); ?></p></body> 
</html>