5

There is a header.php file and it contains some php codes that return HTML. I know I can use require, include to echo the results, but what I want to do is to store its processed output string into a variable.

In a page, I used:

$headerHTML=file_get_contents('header.php');

Then I got the PHP code output rather than the processed HTML output. I know adding http:// would help. But I prefer to keep using relative path, how can I tell the function to treat the php file correctly?

Note: I would like to continue to use this statement file_get_contents rather than using ob_start() if possible.

4 Answers 4

5

I'd rather use require() wrapped inside ob_start() and ob_get_clean(). I am sure there is nothing wrong with this approach.

Sign up to request clarification or add additional context in comments.

3 Comments

It seems this may be the best way.
Unless you need to do it in a part of code already wrapped in ob_
ob_ calls can be nested; in which case the current buffer contents are returned.
1

Don't use eval() - it's evil!

Use the relative local path an automatically map it to a absolute URL.

Comments

1

If URL wrappers are enabled and you want the output of header.php (and you don't want to keep session state) you could use $headerHTML=file_get_contents('http://yourdomain.tld/path/to/header.php');, though why you would want to do such a thing eludes me. Are you sure you're not trying to do something that could easily be solved by using templates and caching?

Comments

0

You can check https://www.php.net/manual/en/function.eval.php#56641, hope it helps.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.