在file_get_contents中获取变量('file.php')[重复]

在file_get_contents中获取变量('file.php')[重复]

问题描述:

This question already has an answer here:

I have a file called file.php with this content:

<?php

$foo = 'hello';

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

In other file called result.php, I call the file.php with the method file_get_contents():

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

But, I can´t print the internal variable $foo in my result.php file. Only appears as:

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

Can I get this result? //print hello in the result.php file

</div>

file_get_contents() reads the specified file byte by byte but does not interpret it's contents. What you are searching for is the include() directive:

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

use include() instead file_get_contents()