Adding included file contents to a variable

php

Solution

Instead of using `include()`, you can use `file_get_contents()` to store it in a variable:

$form = file_get_contents('sidebar.inc.php');

That won't parse the PHP before storing it in your variable so another option is a solution posted here from the include manual:

$string = get_include_contents('sidebar.inc.php');

function get_include_contents($filename) {
   if (is_file($filename)) {
     ob_start();
     include $filename;
     $contents = ob_get_contents();
     ob_end_clean();
     return $contents;
   }
   return false;
}

Problem

I am trying to add a the contents of an include file to a variable. For example I have a variable called $form: ``` $form = 'Hello world'; $form .= include('sidebar.inc.php'); ``` Is there another way to do this? Because the method noted above is causing me some problems...

Original source

Related problems