获取file_Get_contents(';file.php';)中的变量


Get variable in file_get_contents('file.php')

我有一个名为file.php的文件,其中包含以下内容:

<?php
$foo = 'hello';
?>
<div id="fileContent">
   <?php echo($foo) ?>
  ...some content here
</div>

在另一个名为result.php的文件中,我使用file_get_contents()方法调用file.php:

<div id="resultContent">
  <?php echo(file_get_contents('file.php')) ?>
</div>

但是,我无法在result.php文件中打印内部变量$foo。仅显示为:

<!--?php echo($q); ?-->

我能得到这个结果吗//在result.php文件

中打印hello

file_get_contents()逐字节读取指定的文件,但不解释其内容。您要搜索的是include()指令:

<div id="resultContent">
  <?php include('file.php') ?>
</div>

使用include()而不是file_get_contents()