-1

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

Funny Frontend
  • 3,807
  • 11
  • 37
  • 56
  • 2
    Use [include()](http://php.net/manual/en/function.include.php). ``file_get_contents`` reads the contents of the file, it does not execute it. – Sander Toonen Dec 11 '15 at 10:13

2 Answers2

4

use include() instead file_get_contents()

Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
Pr0100
  • 136
  • 4
4

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>
maxhb
  • 8,554
  • 9
  • 29
  • 53