使用php包含在echo中

时间:2013-08-19 12:56:46

标签: php

我需要回复一下php include。我试过以下但是都没有用?

echo "<td>include'file.php'</td>";
echo "<td><?php include/file.php'?></td>";

为什么这不起作用以及如何实现?

5 个答案:

答案 0 :(得分:6)

你无法在php块中启动新的php块。

您可能会这样做:

echo "<td>";
include 'file.php';
echo "</td>";

答案 1 :(得分:4)

您没有echo个附带的文件,include表示您只是在page1中嵌入page2的源代码,如果您echopage1上,您加入page2以及page1是否有类似

的内容

echo 'hello';

当您加入page2时,这会在page1上回复。

因此,您只需使用include 'file.php';即可,并echo上使用file.php

另外,我想告诉你,我确定你正在做一些通常不会做的事情,例如,为什么你需要在一个td标签中包含一个文件?该文件是否用于某些string / int输出?

答案 2 :(得分:1)

您将所有内容都评估为字符串。你需要做这样的事情。

echo '<td>', file_get_contents('file.php') ,'</td>';

答案 3 :(得分:1)

如果包含文件返回某些内容,则按以下方式执行:

echo '<td>'.include('file.php').'</td>';  // highly unlikely though.

更可能正确的做法是:

echo '<td>';
include('file.php');
echo '</td>';  

如果包含的文件只是文本,请使用file_get_contents()

答案 4 :(得分:0)

ob_start();
include 'file.php';
$output = ob_get_clean();

这样就可以评估php并将所有输出保存到变量