Search code examples
phpechophp-include

php include into a variable then echo that variable


I want to include a file entirely into a variable. So that I can call this var multiple times and keep the code as clean as possible. But when I echo the var it only returns a 1 and when I use the include on itself it output the entire file.

I want to output the included file and run all php code inside it.

So what am I doing wrong here.

default.php

$jpath_eyecatcher = (JURI::base(). "modules/mod_eyecatcher/tmpl/content/eyecatcher.php");
$jpath_eyecatcher_path = parse_url($jpath_eyecatcher, PHP_URL_PATH);
ob_start();
$eyecatcher = include ($_SERVER['DOCUMENT_ROOT'] . $jpath_eyecatcher_path);
ob_end_clean();


echo $eyecatcher . '<br>';

include ($_SERVER['DOCUMENT_ROOT'] . $jpath_eyecatcher_path);

echo output is

1

include output is

eyecatchertype = 2 
fontawesome
envelope-o
insert_emoticon
custom-icon-class
128
images/clientimages/research (1).jpg
top
test

Thanks for the help!


Solution

  • Use file_get_contents or ob_get_clean, like so:

    ob_start();
    include ($_SERVER['DOCUMENT_ROOT'] . $jpath_eyecatcher_path);
    $eyecatcher = ob_get_clean();