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.
Return Values ¶ The function returns the read data or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false . Please read the section on Booleans for more information.
The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.
file — Reads entire file contents into an array of lines. file_get_contents — Reads entire file contents into a string.
PHP Write to File - fwrite() The fwrite() function is used to write to a file. The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written.
I'd rather use require()
wrapped inside ob_start()
and ob_get_clean()
. I am sure there is nothing wrong with this approach.
Don't use eval()
- it's evil!
Use the relative local path an automatically map it to a absolute URL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With